This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • 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
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();
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 = != -1) {
out.write(buf, 0, bytesRead);

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 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
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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!