English 中文(简体)
Java File Transfer getting stuck halfway
原标题:

I am trying to send a file (an image sent as a byte array) with the client and then the server should receive said byte array to make further use of it. However when I click on the "send" to send the image the file transfer starts (as I get a sentImage.jpg in my Desktop) but it gets stuck for some reason I can t figure out and the image never gets correctly sent.

Here s the part that receives from the server (it already accepted the connection):

 public void run(){
   try {
          byte[] receivedData = new byte[1024];
          BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
         // while(bis.read() != -1){
          s.acquireUninterruptibly();
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\Users\Admin\Desktop\sentImage.jpg"));
          while ((incoming = bis.read(receivedData)) != -1) {
              bos.write(receivedData, 0, incoming);
          }
          s.release();
          n.release();
          bis.close();
          bos.flush();
        // }
      } catch (IOException e) {
          e.printStackTrace();
      }
}

and the client is sending here:

public void sendImageResult() {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        int inside = 0;

                        Socket socket = new Socket("localhost", 4444);

                        File myImageFile = new File("C:\Users\Admin\Desktop\test.jpg");
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myImageFile));
                        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream( ));
                        byte[] byteArray = new byte[1024];
                        while ((inside = bis.read(byteArray)) != -1){
                            bos.write(byteArray,0,inside);
                        }
                        bis.close();
                        bos.flush();
                    } catch (UnknownHostException ex) {
                        System.out.println("No se pudo establecer la conexión.");
                        ex.printStackTrace();
                    } catch (FileNotFoundException fnf){
                        fnf.printStackTrace();
                    } catch(IOException ioe){
                        ioe.printStackTrace();
                    }

                }
            }).start();
        }
问题回答

It does not appear that the OutputStream (bos) that is used to write to disk is being closed. This could lead to unexpected results.

As jt said, the OutputStream writing to disk is not being closed, but neither is the OutputStream being used to send the data, nor is the Socket being closed from the sending side. The sending side may be buffering the data at the tcp level, waiting for more bytes before sending the last packet. You are calling flush, but that can be ignored, it s not guaranteed to work like you expect. Another thing to try is calling shutdownOutput on the Socket and seeing if that forces it to flush. You can also try setTcpNoDelay(true) when you open the Socket. If none of that works, get a tcp trace program (I like tcpdump) and use it to see if the packets are actually being sent, it will at least narrow it down to either the send or receive end of things.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签