Reputation: 2001
Following java code is being used to download a requested log file throgh a web application:
protected HttpServletResponse response;
....
response.setContentType("application/octet-stream");
String filename = OrgName + ".log";
response.setHeader("Content-Disposition", "attachment; filename= " + filename);
OutputStream os = response.getOutputStream();
os.write(getFile());
os.close();
Problem comes when OrgName
contains a space like "Xyz Pvt Ltd", in this case file will be download with name "Xyz" rather than "Xyz Pvt Ltd.log".The part of name after 1st space is ignored. Please note that the file is downloaded correctly, it is only the name which is not showing up correctly. Is there anything I am doing wrong? or Is it a standard behavior?
Environment: Struts 2, Jboss 5.1.0, Mozilla Firefox 3.5.3
Upvotes: 9
Views: 10514
Reputation: 17903
I think I found your problem. Just set the file name string as quoted
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
This should solve your problem.
Upvotes: 19
Reputation: 1486
I think you would have to use a encoding for spaces You can look into apache base64 encoder, I remember that spaces get encoded to %20% and thus on decoding you would be able to retrieve the filename with spaces.
Upvotes: 0