• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to change default "save as" name in downloading files from the server  RSS feed

 
sarah cruz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i have this problem, i have a button which says download - i use this code to download my file:

==========
final ServletOutputStream out = response.getOutputStream();
response.setContentType("application/octet-stream");
BufferedInputStream is;

File file = new File(getServletConfig().getServletContext().getRealPath("/") + imageList.get(0).getImagePath());
is = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
is.close();
out.close();
===============

however when i am asked to save the file, the default filename is the name of my servlet where the the download button is - without even a file extension. how can i change that default to my desired default download name and file extension? for example, i have a button in NameServlet.java. i get to save the file as NameServlet and i have to rename it to NameServlet.jpg to view the files! that's not very user friendly...huhu...

i hope someone can help. thank you!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up the "Content-Disposition" header.
It does two things.
  • Allows you to suggest whether the browser opens the associated application inline or as a separate app.
  • Allows you to set the filename


  • There is an example in our JSP FAQ.
    Look for JspAndExcel.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!