|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > File download canceled |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|