1

I have a requirement, where i need to process a set of files and create a compressed zip file out of it and then use it for download. I am using a Servlet for downloading that file, but the download takes quite sometime. So i want the user to know that the servlet is processing the request through a print writer output messsage instead of showing him a blank screen.But everytime i use a printwriter to write something to the screen, the message takes a lot of time to show on the screen and the file doesnt download. How can i achieve this? Any ides? Thanks.

Here's my code

OutputStream oStream = null;
    DataInputStream dInput = null;
    File file = new File(("PATH"));
    int length = 0;
    try{
        DownloadServerLogs.processLogs();
        oStream = res.getOutputStream();
        res.setContentType("application/zip");
        res.setContentLength((int)file.length());
        res.setHeader("Content-Disposition",
                         "attachment;filename=\"" + file.getName() + "\"" );
        byte[] bbuf = new byte[BYTES_DOWNLOAD];
        dInput = new DataInputStream(new FileInputStream(file));

        while ((dInput != null) && ((length = dInput.read(bbuf)) != -1))
        {
            oStream.write(bbuf,0,length);
        }
        dInput.close();
        oStream.flush();
        oStream.close();
    }catch(Exception e){
        Utility.getLogger().error(e.getMessage(), e);
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hitatichi
  • 73
  • 2
  • 6

2 Answers2

0

I guess its better to display the message to the client by using something like Javascript (assuming you are using AJAX to invoke the servlet).

anon
  • 1,071
  • 2
  • 8
  • 19
  • I wasnt using an Ajax call. I am hitting the servlet directly. This is just a small utility for processing error logs. – Hitatichi Apr 14 '11 at 22:01
0

Well, it's probably not that safe to do it that way. I'd have a look at the answer below. Not that it's your exact problem, but might be similar.

Most efficient way to create InputStream from OutputStream

Effectively, you are reading and writing on the same thread and my guess is that with the addition of the PrintWriter you are getting deadlocked somewhere.

Community
  • 1
  • 1
bwalenz
  • 332
  • 1
  • 3