Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Canceling file download??

 
Milan Tomc
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!")
outStream.flush();

return null;

-----------

private void setContentType(HttpServletResponse resp, FileInfo info) {
...
if (extension.equals("pdf")){
resp.setContentType("application/pdf");
}else if (extension.equals("doc")){
resp.setContentType("application/msword");
}else if (extension.equals("txt")){
resp.setContentType("html/text");
}else if (extension.equals("xls")){
resp.setContentType("application/vnd.ms-excel");
}else{
resp.setContentType("application/x-file-download");
}

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

----------------------

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic