In action method (J开发者_如何学GoSF) 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 IllegalStateException
s 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.
You just have to set the mime type to application/x-pdf
into your response. You can use the setContentType(String contentType) method to do this in the servlet case.
In JSF/JSP you could use this, before writing your response:
<%@ page contentType="application/x-pdf" %>
and response.write(yourPDFDataAsBytes());
to write your data.
But I really advise you to use servlets in this case. JSF is used to render HTML views, not PDF or binary files.
With servlets you can use this :
public MyPdfServlet extends HttpServlet {
protected doGet(HttpServletRequest req, HttpServletResponse resp){
OutputStream os = resp.getOutputStream();
resp.setContentType("Application/x-pdf");
os.write(yourMethodToGetPdfAsByteArray());
}
}
Resources :
- mimeapplication.net - pdf
- Javadoc - ServletResponse
- Javadoc - HttpServlet
When sending raw data to the browser using JSF, you need to extract the HttpServletResponse
from the FacesContext
.
Using the HttpServletResponse
, you can send raw data to the browser using the standard IO API.
Here is a code sample:
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)).
}
Also, here are some other things you might want to consider:
- Set the content type in the HttpServletResponse to inform the browser you're sending PDF data: response.setContentType("application/pdf");
- Inform the FacesContext that you sent data directly to the user, using the context.responseComplete() method. This prevents JSF from performing additional processing that is unnecessary.
精彩评论