17

次のクラス (わかりやすくするために単純化されています) を使用して、Struts Web アプリケーションで画像をダウンロードしています。スペースを含む名前をカットする Firefox を除くすべてのブラウザで問題なく動作します。つまり、 space.pdf を含むファイルは firefox ではfileとしてダウンロードされますが、クロムでは IE7 IE6 はspace.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を使用していることに注意してください。

4

3 に答える 3

36

ファイル名は引用符で囲まれた文字列にする必要があります。( RFC 2616 のセクション 19.5.1 によると)

response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
于 2008-10-07T10:32:29.093 に答える
1

ファイル名を URL エンコードしますか?

または、少なくとも %20 をスペース文字に置き換えます。

(うまくいくかわかりませんが、やってみてください)

ファイル名も引用符で囲みましたか?

于 2008-10-07T10:26:22.413 に答える
0

これはFirefox 3のセキュリティ機能だと思います。

どうぞ

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

それは異なりますが、それは役立つかもしれません:)

楽しみ

于 2008-10-07T10:25:47.610 に答える