开发者

Java file transfer

开发者 https://www.devze.com 2023-03-08 12:46 出处:网络
I am using the code in Java TCP socket: data transfer is slow, works nice but i have one problem on the receiving part it reads the data but doesn\'t exit the while loop, don\'t really know why, code

I am using the code in Java TCP socket: data transfer is slow, works nice but i have one problem on the receiving part it reads the data but doesn't exit the while loop, don't really know why, code is exactly the same.

ok so the code is like this client side:

uploadFile.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ev){
            try{
                JFileChooser chooser = new JFileChooser();
                File file=null;
                int returnVal = chooser.showOpenDialog(UserWindow.this);
                if (returnVal == JFileChooser.APPROVE_OPTION)
                    file = chooser.getSelectedFile();
                FileInputStream fis = new FileInputStream(file);
                OutputStream os= Client.socket.getOutputStream();
                int packetSize=65536;
                byte[] buffer = new byte[packetSize];
                Client.out.writeObject("upload file");
                int read=0;
                do{
                    os.write(buffer, 0, read);
                    System.out.println(read);
                }while((read = fis.read(buffer))!=-1);
                os.flush();
                System.out.println("sent");
                fis.close();
            }
            catch(Exception ex){
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null,"File send error: "+ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);}
        }
    });

server side:

else if(message.equals("upload file")){
                try{
                    FileOutputStream fos=new FileOutputStream("doc.pdf");
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    int packetsize=65536;
                    byte[] buffer = new byte[packetsize];
                    InputStream is =socket.getInputStream();
                开发者_如何学JAVA    int read=0;
                    do{
                        bos.write(buffer,0,read);
                        System.out.println(read);
                    }while((read = is.read(buffer))!=-1);
                    System.out.println("received");
                    bos.close();
                    fos.close();


                }catch(Exception ex){ex.printStackTrace();}
            }


The client code will not end until you send it the data it expects (which, as, the asker of the question you linked said, is a lot).

And, the code that the asker of the question you linked is not complete, please post your complete code.


  1. Your 'do' loops should be 'while' loops. All you are accomplishing via the 'do' form is a wasted first operation where the length is zero. It isn't wrong but it's pointless.

    while ((count = in.read(buffer)) > 0)
      out.write(buffer, 0, count);
    
  2. The server-side loop won't terminate until the client closes the connection, which you never seem to be doing. That's what causes read() to return -1.

  3. You are doing all this in an actionPerformed() method, which executes in the AWT thread, so it will block the GUI. You must use a separate thread, not the AWT thread, for long-running operations.

  4. If you need to keep the connection open after sending the file you will need to send the length of the file ahead of the file, e.g. viaDataOutputStream.writeLong(). Then modify the read loop to read exactly that many bytes:

    final long total = dis.readLong();
    long current = 0;
    while (current < total &&
           (count = dis.read(buffer, 0, total-current > buffer.length ? buffer.length : (int)(total-current))) > 0)
    {
      out.write(buffer, 0, count;
      current += count;
    }
    

(modulo bugs, check the parentheses especially)

0

精彩评论

暂无评论...
验证码 换一张
取 消