问题描述
我正在使用OutputStream创建要下载的pdf文件,如下所示:
byte[] infoFile = info.getBytes();
String infoName = info.getFileName();
String contentType = info.getContentType();
response.setContentType(contentType);
response.setHeader("Content-disposition","attachment;filename=\"" + infoName + "\"");
response.setContentLength(infoFile.length);
// till Now no problem,the file name is ok
OutputStream out = response.getoutputStream();
out.write(infoFile);
//here,as soon as the prevIoUs line is executed a file is generated with wrong characters
// ex. D:__Profiles__User__Downloads__infoFile.pdf
此处生成的文件类似于“ D:__ Profiles__User__Downloads__infoFile.pdf” 而我希望文件“ D:\ Profiles \ User \ Downloads \ infoFile.pdf”
怎么了?
解决方法
怎么了?
您希望Content-disposition标头中的文件名具有路径信息。
收件人必须不能写到除以下位置以外的任何位置 他们特别有权获得的一种。为了说明 问题,请考虑能够覆盖的后果 众所周知的系统位置(例如“ / etc / passwd”)。一种策略 要做到这一点,是永远不要信任文件夹名称信息 filename参数,例如,通过去除除最后一个以外的所有内容 路径段,仅考虑实际的文件名(其中“ path 段”是字段值的组成部分,由 路径分隔符“”和“ /”)。
同样,在Mozilla docs
文件名始终是可选的,并且不能被应用程序盲目使用:路径信息应删除,并应转换为服务器文件系统规则。
基本上,您只应指定infoFile.pdf
。用户将文件保存在哪个目录下。