I have written a Servlet program to download a file. After getting the response form the server, I print the contents of the response like this:
HttpEntity httpEntity = response.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.i("THE RESPONSE",(sb.toString()));
However, the contents of the file printed is not complete. Only the partial file gets printed. Is is beacuse the server is sending me only a partial file? If yes, then how do you ensure that I get the complete file.
Here is my servlet:
response.setContentType("text/plain");
response.setHeader("Content-Disposition","attachment;filename=downloadname.txt");
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/downloadtest.txt");
int read=0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while((read = is.read(bytes))!= -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
This is how I make the request:
HttpResponse response = httpClient.execute(getRequest);
The size of the file to be downloaded is 208Kb and the value of BYTE_DOWNLOAD is 1024*1024.
How do I solve this problem?