0

I got this client application that sends my file fully to server. But I want it to send file in chunks. Here is my client code:

byte[] fileLength = new byte[(int) file.length()];  

        FileInputStream fis = new FileInputStream(file);  
        BufferedInputStream bis = new BufferedInputStream(fis);

        DataInputStream dis = new DataInputStream(bis);     
        dis.readFully(fileLength, 0, fileLength.length);  

        OutputStream os = socket.getOutputStream();  

        //Sending size of file.
        DataOutputStream dos = new DataOutputStream(os);   
        dos.writeLong(fileLength.length);
        dos.write(fileLength, 0, fileLength.length);     
        dos.flush();  

        socket.close();  

So how can I make client send my file in chunks? Thanks in advance.

Charles
  • 48,924
  • 13
  • 96
  • 136
Rohit Malish
  • 3,141
  • 12
  • 44
  • 66
  • Why do you want to chunk the data ? A Chunk is generally used when the length of the data to be transmitted is not known in advance. – Santosh Jul 12 '12 at 11:32
  • Im trying to send large files – Rohit Malish Jul 12 '12 at 11:33
  • Also, since you are using raw sockets, if you chunk the data, the server side know should be aware of this or else, use standard HTTP protocol. – Santosh Jul 12 '12 at 11:34
  • @Santosh I think he is just using the wrong title. it is not sending in chunks but rather reading the file in chunks. –  Jul 12 '12 at 11:34
  • My server already reads data in chunks, i need to chop the file to chunks before sending it, because I cant send huge files that way – Rohit Malish Jul 12 '12 at 11:36
  • @A.J. Rohit is writing in chunks only thing is that he is using a single chunk. In the code he is sending the chunk length first and then its content. He will not work unless the application on listening socket is aware if this _protocol_. – Santosh Jul 12 '12 at 11:38
  • For context, this is a followup to his question from half an hour ago: http://stackoverflow.com/questions/11449398/cant-send-large-files-over-socket-in-java – Marko Topolnik Jul 12 '12 at 11:39

1 Answers1

1

Try to send the file from client in parts, something like

int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

and reassemble it on the server.

Apache Commons supports streaming, so it may help.

yohlulz
  • 123
  • 1
  • 9
  • I already got that in my server side, and it works. I need my clien ( the one that sends the file) to send the file in chunks and not fully – Rohit Malish Jul 12 '12 at 11:43
  • @RohitMalish, the example code here actually a client side code. – Santosh Jul 12 '12 at 11:53
  • Ok I now tried that code but it sends my files half empty or empty – Rohit Malish Jul 12 '12 at 12:11
  • And it also crashes my browser – Rohit Malish Jul 12 '12 at 12:17
  • @RohitMalish Maybe the TCP connection is closed to early by the server and so not all data is transferred. Try [socket.shutdownOutput()](http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html#shutdownOutput%28%29) before socket.close or try to send a message back from the server when a chunk is fully transferred so the client can send another piece. Hope it helps – yohlulz Jul 12 '12 at 12:24