Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 16th, 2003, 03:54 PM
waldthau waldthau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: Colorado
Posts: 46 waldthau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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

Reply With Quote
  #2  
Old April 21st, 2003, 08:58 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Quote:
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.

Reply With Quote
  #3  
Old April 21st, 2003, 01:20 PM
waldthau waldthau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: Colorado
Posts: 46 waldthau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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.

Reply With Quote
  #4  
Old April 22nd, 2003, 08:29 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > File download canceled


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway