how to send data in chunks from server and client in java

Posted on 2011-03-07
Last Modified: 2013-11-23
if the file i request is too large, the client need to wait very long. what does to be modify from my code in order to receive the data in chunks.
e.g. transmit 100MB into 10MB each chunk in order for me to process
int len=0;
        byte [] mybytearray  = new byte [buffer];
        InputStream is = connection.getInputStream();
        FileOutputStream fos = new FileOutputStream("default.amc");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

 while(( > 0)
         long end = System.currentTimeMillis();

Open in new window

Question by:jackson_duffy
  • 5
  • 4
LVL 92

Expert Comment

ID: 35054550
To support sending data in chunks would also require modifying the client to support receive the data in chunks
Changes to the server would be to support specifying the start byte and the number of bytes to send.

>  while(( > 0)

btw, that should be:

 while(( >= 0)
LVL 92

Expert Comment

ID: 35054598

Author Comment

ID: 35054693
my server side codes are shown below.
with my current codes. i only able to see the received file at the client folder when the file is received fully
Please advise any changes needed. thanks
byte[] bytes = new byte[65536];
                File file = new File("test.txt");
                FileInputStream bis = new FileInputStream(file);
                BufferedInputStream in = new BufferedInputStream(bis);
                OutputStream out = connection.getOutputStream();
                DataOutputStream dout = new DataOutputStream(out);
                //now send the length (as a simple long)
                long numToSend = file.length();
                //now send the file contents
                long numSent = 0;
                while(numSent < numToSend) {
                   long numThisTime = numToSend - numSent;
                   numThisTime = numThisTime < bytes.length ? numThisTime : bytes.length;
                   int numRead =, 0, (int) numThisTime);
                   if(numRead ==-1 ) break;
                   numSent += numRead;

Open in new window

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.


Author Comment

ID: 35065313
any amends to my codes is needed??
LVL 92

Expert Comment

ID: 35065693
not sure what you need.

Author Comment

ID: 35065726
i am sorry.
what i am trying to ask is how to i send and receive file in chunks using a server and client architecture. and what do i have to take note of especially for the server side which is send data and the client side which is to receive data.
as you mentioned, for the send data portion, do the server have to send data in chunks too in order for the client to receive the data in chunks? or what should be done?
LVL 92

Expert Comment

ID: 35065874
first step would be to design the protocol used by the client and server to communicate. Not much point writing any code until you decide how the two are going to communicate.

Author Comment

ID: 35066122
the protocol is udp
LVL 92

Accepted Solution

objects earned 500 total points
ID: 35066377
I meant the application protocol that the client and server will use when communicating.

heres an example od a simple udp client server setup.

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 81
jdbc error in jsp application 20 45
check java version using powershell 13 92
Adding multiple JVM environments to RedHat 6 7 21
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question