如何将字节 [] 作为 pdf 发送到 Java Web 应用程序中的浏览器? [英] How to send byte[] as pdf to browser in java web application?

查看:13
本文介绍了如何将字节 [] 作为 pdf 发送到 Java Web 应用程序中的浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在操作方法(JSF)中,我有如下内容:

In action method (JSF) i have something like below:

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

如何将 byte[] 作为 pdf 发送到浏览器?

How to send byte[] as pdf to browser ?

推荐答案

在 action 方法中,您可以通过 ExternalContext#getResponse().然后您至少需要将 HTTP Content-Type 标头设置为 application/pdf 并将 HTTP Content-Disposition 标头设置为 attachment(当你想弹出一个另存为对话框时)或inline(当你想让浏览器自己处理显示时).最后,您需要确保您调用 FacesContext#responseComplete() 之后避免 IllegalStateException 飞来飞去.

In the action method you can obtain the HTTP servlet response from under the JSF hoods by ExternalContext#getResponse(). Then you need to set at least the HTTP Content-Type header to application/pdf and the HTTP Content-Disposition header to attachment (when you want to pop a Save As dialogue) or to inline (when you want to let the webbrowser handle the display itself). Finally, you need to ensure that you call FacesContext#responseComplete() afterwards to avoid IllegalStateExceptions flying around.

启动示例:

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 循环中编写它即可.

That said, if you have the possibility to get the PDF content as an InputStream rather than a byte[], I would recommend to use that instead to save the webapp from memory hogs. You then just write it in the well-known InputStream-OutputStream loop the usual Java IO way.

这篇关于如何将字节 [] 作为 pdf 发送到 Java Web 应用程序中的浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆