2

I split a file in Path textfield ( use JFileChooser to setText) and send it throughough socket 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();
            }
        }

    }

When click receive button on Server, it'll exec 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();
        }
    }

But it will be receive one part per receive button click. I try to add isContinue property/method in class FileEvent but it depend on number of parts after splitted. So how do i loop it?

tajk
  • 23
  • 6

0 Answers0