gpt4 book ai didi

java - Firefox在Struts应用程序中剪切名称中包含空格的文件

转载 作者:太空狗 更新时间:2023-10-29 22:37:44 24 4
gpt4 key购买 nike

我正在使用下一个类(为了便于理解而进行了简化)在 struts web 应用程序中下载图像。它在除 firefox 之外的所有浏览器中都运行良好,firefox 会删除包含空格的名称。也就是说:file with spaces.pdf 在 firefox 中下载为:file 而在 chrome 中,IE7 IE6 下载为 file with spaces.pdf.

public class Download extends Action {
private static final int BUFFER_SIZE = 4096;

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String filename = "file with spaces.pdf";
File file = ... // variable containing the file;
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(getMimeType(request, file));
response.setHeader("Content-Type", getMimeType(request, file));
response.setHeader("Content-Disposition","attachment; filename="+ filename);
InputStream is = new FileInputStream(file);
sendFile(is, response);
return null;
}

protected String getMimeType(HttpServletRequest request, File file) {
ServletContext application = super.servlet.getServletContext();
return application.getMimeType(file.getName());
}

protected void sendFile(InputStream is, HttpServletResponse response) throws IOException {
BufferedInputStream in = null;
try {
int count;
byte[] buffer = new byte[BUFFER_SIZE];
in = new BufferedInputStream(is);
ServletOutputStream out = response.getOutputStream();
while(-1 != (count = in.read(buffer)))
out.write(buffer, 0, count);
out.flush();
} catch (IOException ioe) {
System.err.println("IOException in Download::sendFile");
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) { ioe.printStackTrace(); }
}
}
}
}

有人知道这里发生了什么吗?请注意,我在 Windows Vista 下使用 firefox 3.0.3。

最佳答案

文件名应该是带引号 的字符串。 (根据 Section 19.5.1 of RFC 2616 )

response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");

关于java - Firefox在Struts应用程序中剪切名称中包含空格的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/177863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com