#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    46
    Rep Power
    14

    File download canceled


    I am using the following code to download files through a JSP. This code works fine unless the user cancels the download after it has begun. Then it seems to stay stuck in the while loop and control does not return to the rest of my application (i.e. no further actions can be taken by the user). I thought the finally clause would execute if the cancel button was pressed, but apparently it does not. Any ideas on how to correct this?

    Code:
    <%
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + request.getAttribute("FileName") + "\"");
    java.io.InputStream is = null;
    try {
        is = (java.io.InputStream)request.getAttribute("File");
        int i;
        while ((i=is.read()) != -1) {
            out.write(i);
        }
    } finally {
        out.flush();
        out.close();
        if (is != null) {
            is.close();
        }
    }
    %>
    - MW
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Then it seems to stay stuck in the while loop...
    Don't assume anything. Put some System.out.prinln statements in and see exactly what the program is doing. I would also think it should be executing the finally block.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    46
    Rep Power
    14
    I did have some println statements in there, but removed them for clarity. I should have said "it does get stuck in the while loop." I also tried catching exceptions to see if any were thrown, but no luck still.

    Thanks.
    - MW
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    This seems to work without getting caught in the loop
    Code:
    <%@ page import="java.io.*" %><%
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + request.getAttribute("FileName") + "\"");
    java.io.InputStream is = null;
    OutputStream out2 = null;
    try {
        out2 = response.getOutputStream();
        is = new FileInputStream(request.getRealPath("matrix.mov"));
        int i;
        while ((i=is.read()) != -1) {
            out2.write(i);
        }
    } finally {
        if(out2 != null) {
             out2.flush();
             out2.close();
       }
        if (is != null) {
            is.close();
        }
    }
    %>
    The implicit "out" variable is a JspWriter. I believe it does some buffering that may be causing your problem.

IMN logo majestic logo threadwatch logo seochat tools logo