wood burning stoves 2.0*
The moose likes Servlets and the fly likes file downoad [browser issue] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "file downoad [browser issue]" Watch "file downoad [browser issue]" New topic
Author

file downoad [browser issue]

Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
Hi,

I'm trying to download the file and it downloads successfully. But problem I'm facing when I get Download File dialog, and click on Open directly without downloading, it doesn't get opened in the IE 6.0 Windows XP SP2 and gives the error

"Cannot find the C:\Documents and Settings\<username>\Local Settings\Temporary Internet Files\Content.IE5\<session>\<filename>.txt file.

Do you want to create a new file?"

I've specified content-type and "Content-Disposition" in servlet and trying to write the data using FileOutputStream.

Now, same is not the case with Mozila. I can open and view the file without any problem..Is this anything that I've to code IE specific in Servlet when downloading the file?

-Bhavin
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
You can't use FileOutputStream to write data to a servlet response.


James Carman, President<br />Carman Consulting, Inc.
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
I tried ServletOutputStream even but same result...If I can't use FileOutputStream then why it gets open in Mozila?

-Bhavin
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Why don't you post some code so that we can see what you're doing?
Ishrayansh Nath
Ranch Hand

Joined: Nov 05, 2004
Posts: 45
This is a sample code,try for it all browsers.it will work fine.

resp.setContentType("application/download");
resp.setHeader("Content-Disposition", "attachment;filename="usercanchangeName + ".format");

OutputStream servletoutputstream = resp.getOutputStream();
byte abyte0[] = new byte[4096];
BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(new File(downloadfilenamewithformat)));
int i;
while ( (i = bufferedinputstream.read(abyte0, 0, 4096)) != -1)
servletoutputstream.write(abyte0, 0, i);

bufferedinputstream.close();
servletoutputstream.flush();
servletoutputstream.close();
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
First of all, don't close the servlet output stream. The container will do that. Second, you might try writing the data to a ByteArrayOutputStream to convert it to a byte[]. Then, you can set the contentLength of the response and I think that might clear up your issues. If IE doesn't know how long the file is, then it might not think you're done.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"Cannot find the C:\Documents and Settings\<username>\Local Settings\Temporary Internet Files\Content.IE5\<session>\<filename>.txt file.


I've seen this happen when the browser's cache is full.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
Actually, when i upload the file i store directly in DB (BLOB). When I download I'm getting data in bytes that I pass to ServletOutputStream.

ServletContext context = servlet.getServletContext();
String contentType = context.getMimeType(docName);

if (contentType != null) {
response.setContentType(contentType);
} else {
response.setContentType("application/octet-stream");
}
response.setHeader("Content-Disposition", "attachement; filename=" + downloadFile + ";");
byte []data = actualData;
response.setContentLength(data.length);
OutputStream servletoutputstream = response.getOutputStream();
servletoutputstream.write(data);

within method it doesn't throw any error..but at this moment Download dialog is not popped up....and finally I end-up with one JSPException....

I want to know when this Dialog should be popped up? And above code is correct for downloading data?

-bhavin
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
well, cache is not full. I've checked it...

-bhavin
 
 
subject: file downoad [browser issue]