Java OutputStream生成错误的目录路径和字符



我正在使用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">

怎么了?

怎么了?

您期望内容处置标头中的文件名应具有路径信息。

来自RFC 6266第4.3节

收件人不得写入除他们有权享有的权利。为了说明问题,考虑能够覆盖的后果已知的系统位置(例如"/etc/passwd"(。一种策略要做到这一点,就永远不要信任filename参数,例如通过剥离除最后一个之外的所有路径段,并且仅考虑实际文件名(其中"pathsegments是字段值的组成部分,由路径分隔符字符"以及"/&"(。

类似地,在Mozilla文档中

文件名始终是可选的,应用程序不得盲目使用:应剥离路径信息,并转换为服务器文件系统规则。

基本上,您应该只指定infoFile.pdf。文件保存在哪个目录中取决于用户

最新更新