Empty space in filename issue while downloading file

淺唱寂寞╮ 提交于 2020-01-03 08:18:07

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8005834/empty-space-in-filename-issue-while-downloading-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!