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

Specifting zip file name in servlet  RSS feed

 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am breezing along zipping up files in my servlet when I find that I cannot specify the name of the zip file!

ServletOutputStream out = response.getOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(out);

response.addHeader("Content-Disposition", "attachment; filename=charts.zip");

The name of the zip file turns out to be the name of the servlet. I don't find any answer in the docs. How can I specify the file name?

Thanks,

Eric
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're Content-Disposition header looks right.
Try setHeader instead of addHeader.
I'm wondering if you've got two headers with the same name going down to the browser.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No dice. Does it make a difference when addHeader is called? Should it be before anything is written to the ServletOutputStream?
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does! I put addHeader before content is written out and now I'm downloading charts.zip. Thank you for your help!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Eric Hamacher:
No dice. Does it make a difference when addHeader is called? Should it be before anything is written to the ServletOutputStream?


That would make sense.
Have you tried it?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!