aspose file tools*
The moose likes Servlets and the fly likes need urgent help on file download servlet problem in IE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "need urgent help on file download servlet problem in IE" Watch "need urgent help on file download servlet problem in IE" New topic
Author

need urgent help on file download servlet problem in IE

Peter W Smith
Greenhorn

Joined: Aug 28, 2002
Posts: 10
I have trouble to make my download servlet work in Internet Explore. I have gone through all the previous postings and done some research on the internet, but still can't figure out the solution. <br>
I have a jsp page called "download.jsp" which contains a URL to download a file from the server, and I wrote a servlet (called downloadServlet) to look up the corresponding file from the database based on the URL and write the file to the HTTP output to send it back to the browser. <br>
the URL in download.jsp is coded like <a href="http://localhost:8100/download/<%= currentDoc.doc_id %>/<%= currentDoc.name %>">
on the browser, it will be sth like <a href="http://localhost:8100/download/87/myfile.doc">, the number 87 is my internal unique number for a file, and "myfile.doc" is the real document name. <br>
in my web.xml, /download/ is mapped to downloadServlet <br>
the downloadServlet.java looks like
-----------------------------------------
tem_name = ... //read DB for file name
if ( tmp_name.endsWith(".doc")) {
response.setContentType("application/msword");
}
else if ( tmp_name.endsWith(".pdf")){
response.setContentType("application/pdf");
}
else if ( tmp_name.endsWith(".ppt")){
response.setContentType("application/vnd.ms-powerpoint");
}
else if ( tmp_name.endsWith(".xls")){
response.setContentType("application/vnd.ms-excel");
}
else {
response.setContentType("application/download");
}
// set HTTP header
response.setHeader("Content-Disposition",
"attachment; filename=\""+ele.upload_location+"\"");
OutputStream os = response.getOutputStream();
//read local file and write back to browser
int i;
while ((i = is.read()) != -1) {
os.write (i);
}
os.flush();
------------------------------------------
Everything works fine in Netscape, but in IE, when I click on the URL, it downloads the "download.jsp" file instead of the "myfile.doc". It will work if I right click and choose "save file as ...".
Please help me.
thanks,
Peter
Peter W Smith
Greenhorn

Joined: Aug 28, 2002
Posts: 10
I fixed it.
sonny kher
Ranch Hand

Joined: Nov 06, 2001
Posts: 83
How? this is so amazing I was just working on a similar problem thought I'd post it on javaranch and yours is the first post i read...pretty funny coincidence
i like your endsWith setContentType idea though you have to keep adding new contents as you go along which can be a bitch....in my case atleast


_ __________________ _ <br />RHCE<br />SCJP<br />??:-(
Peter W Smith
Greenhorn

Joined: Aug 28, 2002
Posts: 10
sonny, I copy my code below
----------------------------------
if ( tmp_name.endsWith(".doc")) {
response.setContentType("application/msword; name=\""+ele.upload_location+"\"");
}
else if ( tmp_name.endsWith(".pdf")){
response.setContentType("application/pdf; name=\""+ele.upload_location+"\"");
}
else if ( tmp_name.endsWith(".ppt")){
response.setContentType("application/vnd.ms-powerpoint; name=\""+ele.upload_location+"\"");
}
else if ( tmp_name.endsWith(".xls")){
response.setContentType("application/vnd.ms-excel; name=\""+ele.upload_location+"\"");
}
else {
response.setContentType("application/download; name=\""+ele.upload_location+"\"");
}
// response.setContentType("application/whatever; name=\""+ele.upload_location+"\"");
// response.setHeader("Content-Disposition",
// "attachment; filename=\""+ele.upload_location+"\";");
response.setHeader("Content-Disposition: ",
"attachment; filename="+ele.upload_location +";");
OutputStream os = response.getOutputStream();
int i;
while ((i = is.read()) != -1) {
os.write (i);
}
...............
---------------------------------
please compare that with my original code. but more importantly, I had IE 5.5 with sp1 which has bugs. I downloaded sp2 and the problem goes away. I didn't test my original code with sp2, so don't know whether the subtle changes also contribute to the fix.
for the "endWith", I guess it doesn't matter much with the new versions of browser, it seems both Netscape and IE of new versions correctly recognize the suffix of the document name in the URL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need urgent help on file download servlet problem in IE