⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recvingfile.java

📁 局域网聊天工具,学习java桌面应用程序的好例子,(代传)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 /////////////////////////////////////////////////////////////////////////                } else {                    initSocket();                    //包编号:文件编号:偏移量                    com.setAdditional(Integer.toHexString(                            flist.getPacketNo()) + ":" +                             Integer.toHexString(fileSelection) + ":" +                            Integer.toHexString(0) + ":");                    //接收文件                    if (flist.getFiles().get(fileSelection).getFileKind() ==                            GlobalConstant.IPMSG_FILE_REGULAR) {                        //发送请求报文                        com.resetFlag(GlobalConstant.IPMSG_GETFILEDATA);                        outputStream.write(com.exportToBuf());                        outputStream.flush();                        /*this.getRegularFile(savePath +                                GlobalVar.FILE_PATH_DELIMITER +                                flist.getFiles().get(fileSelection).getFileName(),                                flist.getFiles().get(fileSelection).getFileLen());*/                        this.getRegularFile(                                this.getPath(savePath,                                flist.getFiles().get(fileSelection).getFileName()),                                flist.getFiles().get(fileSelection).getFileLen());                                            //接收目录                    }                     else if (flist.getFiles().get(                            fileSelection).getFileKind() ==                            GlobalConstant.IPMSG_FILE_DIR) {                        //发送请求报文                        com.resetFlag(GlobalConstant.IPMSG_GETDIRFILES);                        outputStream.write(com.exportToBuf());                        outputStream.flush();                        this.getDir();                    }                    closeSocket();                    if(!cancelNow)flist.getFiles().get(                            fileSelection).setIsTransfered(true);                }                exitNormal();            } catch (IOException ex) {                System.out.println("error");            }        }                private String getPath(String path,String filename){            if(replace)return path+GlobalVar.FILE_PATH_DELIMITER+filename;            else {                while(new File(path+GlobalVar.FILE_PATH_DELIMITER+filename).exists())                    filename=GlobalConstant.DEFAULT_FILE_PREFIX+filename;            }            return path+GlobalVar.FILE_PATH_DELIMITER+filename;        }         private void getRegularFile(String path, long fileLen) {            if(!jToggleButton1.isSelected())                appendMsg("接收文件 : "+path);            byte[] buf = new byte[GlobalConstant.DEFAULT_F_LENGTH];            int read = -1,readonce=GlobalConstant.DEFAULT_F_LENGTH;            long rest = fileLen;            try {                fileOut = new DataOutputStream(                        new BufferedOutputStream(                        new BufferedOutputStream(                        new FileOutputStream(path))));                                //初始化图形显示的一些参数                jLabel1.setText("0%");                long percent=(fileLen/100>GlobalConstant.DEFAULT_F_LENGTH)?                    fileLen/100:GlobalConstant.DEFAULT_F_LENGTH,                    recvPercent=percent;                int counter=1;                //**                while (true) {                    if (inputStream != null) {                        if(readonce>rest)readonce=(int) rest;                        read = inputStream.read(buf,0,readonce);                    }                    if (read == -1) {                        break;                    }                    rest -= read;                    fileOut.write(buf, 0, read);                                        //如果开启了显示                    if(!jToggleButton1.isSelected()){                        if(fileLen-rest>recvPercent){                            recvPercent+=percent;                            updatePercent up=new updatePercent();                            up.percent=counter;                            SwingUtilities.invokeLater(up);                            counter++;                        }                    }                    if (rest <=0) {                        jLabel1.setText("100%");                        break;                    }                }                fileOut.flush();                fileOut.close();            } catch (IOException ex) {            }        }        private void getDir() {            String path = savePath;            /*new File(path).mkdir();*/            int depth = 0;            FileNode fnode;            do {                fnode = this.getHeader();                if(fnode==null)break;                switch (fnode.getFileKind()) {                    case GlobalConstant.IPMSG_FILE_REGULAR:                        this.getRegularFile(path +                                GlobalVar.FILE_PATH_DELIMITER +                                fnode.getFileName(),                                fnode.getFileLen());                        break;                    case GlobalConstant.IPMSG_FILE_DIR:                        if(depth==0)path=this.getPath(path, fnode.getFileName());                        else path = path +                                GlobalVar.FILE_PATH_DELIMITER +                                fnode.getFileName();                                                if(!jToggleButton1.isSelected())                            appendMsg("进入目录 : "+path);//                        System.out.println("进入目录 : "+path);                        new File(path).mkdir();                        depth++;                        break;                    case GlobalConstant.IPMSG_FILE_RETPARENT:                        if(!jToggleButton1.isSelected())                            appendMsg("退出目录 : "+path);                        path = this.getParentDir(path);                        depth--;                        break;                    default:                        System.out.println("wrong file");                }            } while (depth > 0);        }        private String getParentDir(String str) {            int len = str.lastIndexOf(GlobalVar.FILE_PATH_DELIMITER);            if (len < 0) return null;            return str.substring(0, len);        }        private FileNode getHeader() {            int i, pos, count;            try {                String head = UtilityNet.readDelimiter(inputStream,100);                if (head == null) return null;                //需要减去header长度域的长度。                count = Integer.parseInt(head, 16)-5;                if(count<=0)return null;                                byte[] headBuf = new byte[count];                if(inputStream.read(headBuf, 0, count)<0)return null;                                head = new String(headBuf,GlobalVar.CHARACTER_ENCODING);                                FileNode fnode = new FileNode();                for (i = 0; i < 3;) {                    if ((pos = head.indexOf(":")) < 0) {                        return null;                    }                    count = 1;                    while (head.charAt(++pos) == ':') {                        count++;                    }                    if (count % 2 == 0) {                        continue;                    }                    switch (i) {                        case 0:                            fnode.setFileName(head.substring(0, pos-1));                            break;                        case 1:                            if (!fnode.setFileLen(head.substring(0, pos-1))) {                                return null;                            }                            break;                        case 2:                            if (!fnode.setFileKind(head.substring(0, pos-1))) {                                return null;                            }                    }                    head = head.substring(pos);                    i++;                }                return fnode;            } catch (IOException ex) {                return null;            } catch (NumberFormatException e) {                return null;            }        }            }        class updatePercent extends Thread{        int percent;        @Override        public void run() {            jLabel1.setText(this.percent+"%");        }            }        private void closeSocket() {        try {            this.outputStream.close();            this.inputStream.close();            this.socket.close();        } catch (IOException ex) {            System.out.println("error1");        }    }        private void initSocket() {        try {            //初始化变量            socket = new Socket(flist.getIp(),                    GlobalConstant.IPMSG_DEFAULT_PORT);            this.outputStream = new DataOutputStream(                    socket.getOutputStream());            this.inputStream = new DataInputStream(                    new BufferedInputStream(                    socket.getInputStream()));        } catch (Exception e) {            System.out.println("error2");        }    }            private void appendMsg(String str){        this.jTextArea1.append(str+GlobalVar.NEW_LINE);        JScrollBar jb = this.jScrollPane1.getVerticalScrollBar();        jb.setValue(jb.getMaximum());    }               private void exitNormal(){        this.dispose();        this.inter.enableVisible();    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -