Java Firefox在Struts应用程序中剪切名称包含空格的文件
我正在使用下一个类(为了便于理解而简化)在struts web应用程序中下载图像。 除firefox外,它在所有浏览器中都能正常工作,firefox可以删除包含空格的名称。这就是说:带有空格的文件.pdf在firefox中下载为:文件,而在chrome中,IE7 IE6下载为带有空格的文件.pdfJava Firefox在Struts应用程序中剪切名称包含空格的文件,java,firefox,struts,Java,Firefox,Struts,我正在使用下一个类(为了便于理解而简化)在struts web应用程序中下载图像。 除firefox外,它在所有浏览器中都能正常工作,firefox可以删除包含空格的名称。这就是说:带有空格的文件.pdf在firefox中下载为:文件,而在chrome中,IE7 IE6下载为带有空格的文件.pdf public class Download extends Action { private static final int BUFFER_SIZE = 4096; pub
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(); }
}
}
}
}
有人知道这里发生了什么吗?注意:我使用的是WindowsVista下的Firefox3.0.3 我相信这是Firefox3的一个安全特性 我们开始 这是不同的,但可能会有所帮助:)
喜欢URL编码文件名吗 或者至少用%20替换空格字符 (我不知道这是否有效,但试试看)
您是否也尝试过在文件名周围加引号?文件名应该是带引号的字符串。(根据)
但您可能想尝试使用%20,而不是先使用简单的正则表达式替换空格。我不知道它是否会工作,因为它是HTML标题中的内容,但5秒钟的工作时间值得一试。我正在使用grails,当我尝试这样做时,我的文件名末尾有一个额外的空间,所以我的代码是:response.setHeader(“content disposition”,“attachment;filename=”${meuArquivo.nome\}),没有空格。
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");