Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty space in filename issue while downloading file

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

like image 521
Umer Hayat Avatar asked Nov 04 '11 06:11

Umer Hayat


1 Answers

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.

like image 188
Santosh Avatar answered Oct 16 '22 04:10

Santosh