0

こんにちは、ファイルをブラウザに送信する(ユーザーにファイルをダウンロードさせる)ための2つの異なる手法を試しました。myfaces wikipage の例を試しました

FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();

    int read = 0;
    byte[] bytes = new byte[1024];

    String fileName = "test.txt";
    response.setContentType("text/plain");

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

    OutputStream os = null;

    StringBuffer stringBuffer1 = new StringBuffer("Java Forums rock");
    ByteArrayInputStream bis1;
    try {
        bis1 = new ByteArrayInputStream(stringBuffer1.toString().getBytes("UTF-8"));

        os = response.getOutputStream();

        while ((read = bis1.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }

        os.flush();
        os.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    FacesContext.getCurrentInstance().responseComplete();

また、PrimeFaces の fileDownload という名前のコンポーネントを使用してみました。どちらも同じ結果になります。

サーバーから応答を受け取りました。応答には、ファイルにあるはずのテキストが含まれています。ヘッダーは次のとおりです。

X-Powered-By    Servlet/3.0, JSF/2.0
Server  GlassFish v3
Content-Disposition attachment;filename="test.txt"
Content-Type    text/plain
Transfer-Encoding   chunked
Date    Thu, 20 May 2010 06:30:20 GMT

私にはこれは正しいように見えますが、何らかの理由でファイルをダウンロードできず、firebug でこの応答を受け取るだけです。

サーバー設定の問題でしょうか? 私はグラスフィッシュ3を使用しています

ありがとう/ステファン

4

1 に答える 1

3

これを同期ではなく (Ajax を使用して) 非同期で要求しているように聞こえます。そうすると、名前を付けて保存ダイアログが表示されることはありません。ファイルのダウンロードは常に同期的に要求する必要があります。つまり、 「プレーンなバニラ」を使用するh:commandLinkh:commandButton、このためにいくつかの ajaxical コマンド コンポーネントの代わりに使用します。

于 2010-05-20T12:18:56.797 に答える