通过TCP套接字发送多个文件

时间:2015-05-30 05:48:45

标签: java sockets tcp

我在Path textfield中拆分文件(使用JFileChooser到setText)并通过套接字Outputstream发送它:

public void sendFile() throws IOException{
        String fullPath = textField.getText();
        File sFile = new File(fullPath);
        Task.doSplit(fullPath, 200*1000L*1000L);
        String[] allParts = Task.getFileDirectory(fullPath);
        int numPairs = allParts.length;
        for (int i = 0; i < numPairs; i++){
            fileEvent = new FileEvent();
            fileEvent.setfName(sFile.getName() + ".part" + i);
            fileEvent.setdDir(dDir);
            File file = new File(fullPath + ".part" + i);
            if (file.isFile()){
                try{
                    DataInputStream dis = new DataInputStream(new FileInputStream(file));
                    int len = (int) file.length();
                    byte[] fileBytes = new byte[len];
                    int read = 0;
                    int numRead = 0;
                    while (read < fileBytes.length && (numRead = dis.read(fileBytes, read, fileBytes.length - read)) >= 0){
                        read = read + numRead;
                    }
                    fileEvent.setStatus("Succes");
                    fileEvent.setfData(fileBytes);
                    fileEvent.setfSize(len);
                    fileEvent.setToContinue(true);
                    dis.close();
                } catch (Exception e){
                    fileEvent.setStatus("Error");
                }
            }
            else {
                System.out.println("Path specified is not pointing to a file");
                fileEvent.setStatus("Error");
            }
            try {
                outStream.writeObject(fileEvent);
                Thread.sleep(1000);
            } catch (IOException e){
                e.printStackTrace();
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }

在服务器上单击“接收”按钮时,它将执行receiveFile():

public void receiveFile(){
        try{
            fileEvent = (FileEvent) inStream.readObject();
            if (fileEvent.getStatus().equalsIgnoreCase("Error")){
                System.out.println("Error occured.... So exitting....");
                System.exit(0);
            }
            String outputFile = fileEvent.getdDir() + "/" + fileEvent.getfName();
            if (!new File(fileEvent.getdDir()).exists()){
                new File(fileEvent.getdDir()).mkdirs();
            }
            dFile = new File(outputFile);
            fos = new FileOutputStream(dFile);
            fos.write(fileEvent.getfData());
            fos.flush();
            fos.close();
            System.out.println("Successfully receive file from client!");
            Thread.sleep(1000);
            //System.exit(0);
        } catch (IOException e){
            e.printStackTrace();
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }

但每次点击按钮会收到一部分。我尝试在类FileEvent中添加isContinue属性/方法,但它取决于拆分后的部件数量。那我怎么循环呢?

0 个答案:

没有答案