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

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

问题描述

在行动方法(JSF)中,我有如下所示的东西:

  public String getFile(){
byte [] pdfData = ...
//如何将byte []作为文件返回给Web浏览器用户?
}

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

解决方案

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



启动示例:

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

//初始化响应。
response.reset(); //某些JSF组件库或某些Filter可能已预先在缓冲区中设置了一些头文件。我们想摆脱它们,否则可能会碰撞。
response.setContentType(application / pdf); //查看所有类型的http://www.iana.org/assignments/media-types。如果需要使用ServletContext#getMimeType()根据文件名自动检测。
response.setHeader(Content-disposition,attachment; filename = \name.pdf\); //另存为弹出魔法在这里完成。你可以给它任何你想要的文件名,这只能在MSIE中工作,它将使用当前的请求URL作为文件名。

//将文件写入响应。
OutputStream output = response.getOutputStream();
output.write(pdfData);
output.close();

//通知JSF不用手中的响应。
facesContext.responseComplete(); //重要!否则JSF将尝试使响应显然会失败,因为它已经写入一个文件并关闭了。
}

表示,如果您有可能获取PDF内容作为 InputStream 而不是一个 byte [] ,我建议使用它来保存内存中的webapp。然后,您可以在通常的Java IO方式循环使用众所周知的 InputStream - OutputStream

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

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

How to send byte[] as pdf to browser ?

解决方案

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.

Kickoff example:

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.
}

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.

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

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