0

私はこれに何時間も取り組んできました:

ボタンが押されたら、XML を生成してユーザーにダウンロードする必要があります。p:fileDownload で primefaces StreamedContent を使用してみましたが、実際のページの .xhtml ソースしか出力されません。ログ ステートメントを使用して StreamedContent の内容を表示してから返すことができ、そこに XML コードを正しく表示できますが、ダウンロードされたファイルには常にページ ソースが含まれています。(私は他の多くの方法も試しました.jpgファイルをダウンロードし、Beanを変更して応答を直接生成し、h:commandButtonのアクションを介してメソッドを呼び出します-常にページソースを取得します!)。

これが私の .xhtml です:

        <p:commandButton value="Create XML Bid/Offer" 
                 id="createXMLButton" 
             disabled="#{portfolioBean.noPortfolioSelected}"
                 ajax="false"
                 icon="ui-icon-arrowthichk-s">
    <p:fileDownload value="#{portfolioBean.order}" />  
</p:commandButton>

(代わりの)

    <h:commandButton value="Create XML Bid/Offer" 
    id="createXMLButton" 
    disabled="#{portfolioBean.noPortfolioSelected}"
     ajax="false"
    icon="ui-icon-arrowthichk-s"
    action="#{portfolioBean.download()}" /> 

そして、バッキング Bean メソッド:

        public StreamedContent getOrder() {
...
}

(代わりの)

        public String download() {
...
} 

メソッド本体は適切な XML を返すことがわかっているため、含めていません。JSF マジックのようなものです。これは設定の問題か何かですか?

ありがとう!

更新 OK - コードからすべてを取り除いたので、これだけです:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Edison Energy Portal</title>
</h:head>
<h:body>
    <h:form id="form">
        <h:commandButton value="Download PDF" action="#{downloadJPG.downloadFile}" />
     </h:form>
</h:body>
</html>

public class DownloadJPG {

// Constants ----------------------------------------------------------------------------------

private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

// Actions ------------------------------------------------------------------------------------

public void downloadFile() throws IOException {

    // Prepare.
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    File file = new File("....", "....");
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        // Open file.
        input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);

        // Init servlet response.
        response.reset();
        response.setHeader("Content-Type", "application/pdf");
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"...\"");
        output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }

        // Finalize task.
        output.flush();
    } finally {
        // Gently close streams.
        close(output);
        close(input);
    }

    // Inform JSF that it doesn't need to handle response.
    // This is very important, otherwise you will get the following exception in the logs:
    // java.lang.IllegalStateException: Cannot forward after response has been committed.
    facesContext.responseComplete();
}

// Helpers (can be refactored to public utility class) ----------------------------------------

private static void close(Closeable resource) {
    if (resource != null) {
        try {
            resource.close();
        } catch (IOException e) {
            // Do your thing with the exception. Print it, log it or mail it. It may be useful to 
            // know that this will generally only be thrown when the client aborted the download.
            e.printStackTrace();
        }
    }
}

}

ソース イメージ ファイルを確認しましたが、破損していません。BalusC のコードは 1 回の試行で機能し、現在の jpg を XML ファイルに置き換え、MIME タイプを変更しましたが、それ以来何も機能していません。Tomcatで何か悪いことが起こったようで、それを取り出したりリセットしたりできません(手動でEclipseを介してTomcatをクリーニングしようとしましたが、ボックスを再起動しました)。

これは本当のポーザーです。

更新:私は日食をきれいに始めました-運が悪いです。私は本当にアイデアがありません。このコードが機能することを知っているので、これはどこかの構成であると考える必要があります (私はそれを見ました)。

4

1 に答える 1

1

他の誰も(強力な でさえBalusC)答えていないことを考えると、コメントとして投稿しています。

ダウンロード方法としては、やはりBalusC blog . 暇があれば少し閲覧する価値があります。

ブラウザがそれを管理する方法に関する問題については、ブラウザの処理に依存しますが、ほとんどの場合、Content-Dispositionヘッダーを尊重する必要があります。例のこの行を変更します

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

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

参照

于 2012-09-20T20:46:48.617 に答える