我正在使用下一个类(为了便于理解而简化)在struts web应用程序中下载图像。 它在每个浏览器中运行良好,但firefox,它切割包含空格的名称。也就是说:带有spaces.pdf的文件在firefox中下载为:文件,而在chrome中,IE7 IE6作为文件下载,带有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。
答案 0 :(得分:35)
文件名应为引用字符串。 (根据Section 19.5.1 of RFC 2616)
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
答案 1 :(得分:1)
URLEncode文件名?
或者至少用%20代替空格字符。
(我不知道这是否有效,但试一试)
您是否尝试过在文件名周围添加引号?
答案 2 :(得分:0)
这是firefox 3的安全功能。我相信。
我们走了
http://support.mozilla.com/tiki-view_forum_thread.php?locale=no&forumId=1&comments_parentId=91513
它有所不同,但可能会有所帮助:)。
享受