0

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?

Nemin Shah
  • 301
  • 1
  • 3
  • 12
  • I don't believe it will make a difference, you might not want to use such a small buffer size in your BufferedReader: http://stackoverflow.com/a/4638989/1247781 – FThompson Jun 06 '13 at 23:10
  • 1
    Also, what does `Log.i` do? I suspect it might truncate such a large amount of text when it formats or logs it. – FThompson Jun 06 '13 at 23:12

0 Answers0