【问题标题】:Firefox cuts files, whose name contains spaces, in a Struts applicationFirefox 在 Struts 应用程序中剪切名称包含空格的文件
【发布时间】:2008-10-07 10:21:26
【问题描述】:

我正在使用下一个类(为了便于理解而进行了简化)在 struts Web 应用程序中下载图像。 它在每个浏览器中都可以正常工作,但 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。

【问题讨论】:

    标签: java firefox struts


    【解决方案1】:

    文件名应该是一个引用字符串。 (根据Section 19.5.1 of RFC 2616

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

    【讨论】:

    • 我正在使用 grails,当我尝试这样做时,我遇到了文件名末尾多余空格的问题,所以这是我的代码: response.setHeader("Content-disposition", "attachment;filename=\"${meuArquivo.nome}\"") 没有空格。
    【解决方案2】:

    URLEncode 文件名?

    或者至少用 %20 代替空格字符。

    (我不知道这是否可行,但请尝试一下)

    您是否尝试过在文件名周围加上引号?

    【讨论】:

    • 但您可能想先尝试使用 %20 而不是空格,然后进行简单的正则表达式替换。不知道它是否会起作用,因为它是 HTML 标头中的内容,但 5 秒钟的工作值得一试。
    【解决方案3】:

    我相信这是 Firefox 3 的安全功能。

    我们来了

    http://support.mozilla.com/tiki-view_forum_thread.php?locale=no&forumId=1&comments_parentId=91513

    这是不同的,但它可能会有所帮助:)

    享受

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多