簡體   English   中英

如何在java web應用程序中將byte []作為pdf發送到瀏覽器?

[英]How to send byte[] as pdf to browser in java web application?

在動作方法(JSF)中我有類似下面的內容:

public String getFile() {
  byte[] pdfData = ...
  // how to return byte[] as file to web browser user ?
}

如何將byte []作為pdf發送到瀏覽器?

在action方法中,您可以通過ExternalContext#getResponse()從JSF引擎下獲取HTTP servlet響應。 然后,您需要至少將HTTP Content-Type標頭設置為application/pdf ,並將HTTP Content-Disposition標頭設置為attachment (當您要彈出“ 另存為”對話框時)或inline (當您希望讓webbrowser處理顯示自己)。 最后,您需要確保之后調用FacesContext#responseComplete()以避免IllegalStateException飛來飛去。

開球示例:

public void download() throws IOException {
    // Prepare.
    byte[] pdfData = getItSomehow();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    // Initialize response.
    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    response.setContentType("application/pdf"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    response.setHeader("Content-disposition", "attachment; filename=\"name.pdf\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead.

    // Write file to response.
    OutputStream output = response.getOutputStream();
    output.write(pdfData);
    output.close();

    // Inform JSF to not take the response in hands.
    facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}

也就是說,如果您有可能將PDF內容作為InputStream而不是byte[] ,我建議使用它來代替從內存中保存webapp。 然后,您可以使用通常的Java IO方式在着名的InputStream - OutputStream循環中編寫它。

您只需將mime類型設置為application/x-pdf到您的響應中即可。 您可以使用setContentType(String contentType)方法在servlet案例中執行此操作。
在JSF / JSP中,您可以在編寫響應之前使用它:

<%@ page contentType="application/x-pdf" %>

response.write(yourPDFDataAsBytes()); 寫你的數據。
但我真的建議你在這種情況下使用servlet。 JSF用於呈現HTML視圖,而不是PDF或二進制文件。

使用servlet,您可以使用:

public MyPdfServlet extends HttpServlet {
    protected doGet(HttpServletRequest req, HttpServletResponse resp){
         OutputStream os = resp.getOutputStream();
         resp.setContentType("Application/x-pdf");
         os.write(yourMethodToGetPdfAsByteArray());
    } 
}

資源:

使用JSF將原始數據發送到瀏覽器時,需要從FacesContext提取HttpServletResponse

使用HttpServletResponse ,您可以使用標准IO API將原始數據發送到瀏覽器。

這是一個代碼示例:

public String getFile() {
    byte[] pdfData = ...

    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    OutputStream out = response.getOutputStream();
    // Send data to out (ie, out.write(pdfData)).
}

此外,您還可以考慮以下其他一些事項:

  • 在HttpServletResponse中設置內容類型以通知瀏覽器您正在發送PDF數據:response.setContentType(“application / pdf”);
  • 使用context.responseComplete()方法通知FacesContext您將數據直接發送給用戶。 這可以防止JSF執行不必要的額外處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM