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.

7
  • 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. Commented Jul 12, 2012 at 11:32
  • Im trying to send large files Commented Jul 12, 2012 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. Commented Jul 12, 2012 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. Commented Jul 12, 2012 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 Commented Jul 12, 2012 at 11:36

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
@RohitMalish, the example code here actually a client side code.
Ok I now tried that code but it sends my files half empty or empty
And it also crashes my browser
@RohitMalish Maybe the TCP connection is closed to early by the server and so not all data is transferred. Try socket.shutdownOutput() 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.