• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

need urgent help on file download servlet problem in IE

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed it.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Peter W Smith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic