File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Canceling file download?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Canceling file download??" Watch "Canceling file download??" New topic

Canceling file download??

Milan Tomc

Joined: Mar 06, 2006
Posts: 17
Hi all,

When downloading files I get this well known "open/save/cancel window" to decide what to do.
Everything works fine. Or should I say, wordked fine, until I tried to cancel a download of a bigger file.
If a file is small (say 20 KB) everything is fine.
If a file is 50 KB or more, when I click cancel I get:

IllegalStateException: getOutputStream() has already been called for this response

My question is why?

ps. "done streaming" is only shown with small files before a click on one of the buttons. With bigger files it is shown after I click "Open". It seems that it streams "to a certain limit" and then waits for user input. x()

code below:
in action:

response.setContentLength( (int) info.getFileSize());
setContentType(response, info); //see below

ServletOutputStream outStream = response.getOutputStream();
dao.getFileData(dataId, outStream);
syso("done streaming!")

return null;


private void setContentType(HttpServletResponse resp, FileInfo info) {
if (extension.equals("pdf")){
}else if (extension.equals("doc")){
}else if (extension.equals("txt")){
}else if (extension.equals("xls")){

resp.setHeader("Content-disposition", "attachment; filename=" + info.getFilename());
resp.setDateHeader ("Expires", (new Date()).getTime()+10000);


I agree. Here's the link:
subject: Canceling file download??
It's not a secret anymore!