java send file using sockets

前端 未结 3 670
长情又很酷
长情又很酷 2020-12-28 11:08

I am trying to send a file from one computer to another using Java. I have written the code below, it works fine if both sender and receiver are started in the same computer

3条回答
  •  长情又很酷
    2020-12-28 11:50

    sender

    Socket sock = new Socket("127.0.0.1", 5991);
            System.out.println("Connecting.........");
            File myFile = new File("/root/qrcode/");
            File[] files = myFile.listFiles();
           OutputStream os = sock.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);
                                DataOutputStream dos = new DataOutputStream(bos);
    
                                dos.writeInt(files.length);
                                long totalBytesRead = 0;
                                int percentCompleted = 0;
                                for(File file : files)
                                {
                                         long length = file.length();
                                         dos.writeLong(length);
    
                                         String name = file.getName();
                                         dos.writeUTF(name);
    
                                         FileInputStream fis = new FileInputStream(file);
                                         BufferedInputStream bis = new BufferedInputStream(fis);
    
                                         int theByte = 0;
                                         while((theByte = bis.read()) != -1)
                                         {
                                            totalBytesRead += theByte;
    
    
                                            bos.write(theByte);
                                         }
                                        //  System.out.println("file read");
                                         bis.close();
                                     }
    
                                    dos.close();
    
    
            //Closing socket  
            sock.close();
    

    receiver

    ServerSocket serverSocket = new ServerSocket(5991);  
    
        while(true) {  
            Socket clientSocket = null; 
            System.out.println("Starting...");
            clientSocket = serverSocket.accept();  
    
            InputStream in = clientSocket.getInputStream(); //used
    
            BufferedInputStream bis = new BufferedInputStream(in);
    
            String dirPath  ;
            dirPath = "/root/NewFolder";
    
            try{
                DataInputStream dis = new DataInputStream(bis);
    
                int filesCount = dis.readInt();
                File[] files = new File[filesCount];
                long f_l = 0;
                int count =0 ;
                long totalBytesRead = 0;
                int percentCompleted = 0;
    
                for(int i = 0; i < filesCount; i++)
                {
                    long fileLength = dis.readLong();
                    String fileName = dis.readUTF();
    
                    f_l = f_l +fileLength;
                    files[i] = new File(dirPath + "/" + fileName);
    
                    FileOutputStream fos = new FileOutputStream(files[i]);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
    
                    int tot = 0;
                    for(int j = 0; j < fileLength; j++) {
    
                        bos.write(bis.read());
                    }
    
                    bos.close();
    
                }
    
            }catch(Exception ex)
            {
                System.out.println("error in socket programming ");
            }
        }
    

提交回复
热议问题