如何从后端Java代码发送PDF文件?

时间:2011-12-14 07:27:49

标签: java jsp pdf servlets download

我有一个JSP文件,有后端帮助类。从后端帮助器我需要将PDF文件作为附件发送到JSP。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

我建议您使用Apache Commons File Upload组件。这可能是最好的方式,而不是重新发明轮子。 ;)

答案 1 :(得分:0)

由于您没有告诉我们您是使用任何MVC框架还是仅使用普通的Servlet,我将继续使用基础知识。

如果您要上传文件,Apache Commons文件上传是最好的库,可以翻译您的multipart/form-data编码HttpServletRequest邮件,并为您提供上传的文件(InputStream格式,最喜欢的。)

由开发人员决定将数据写回您选择的持久存储中。

相反,它是取文件,获取适当的MIME类型,内容长度(文件大小)和文件数据(如果可能的话InputStream)并将其渲染回{{1} })。

此代码(完全正常并由我编写)确实将文件作为附件/内联。

HttpServletResponse

最重要的方法是import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; /** * @author Buhake Sindi (The Elite Gentleman) * @since 01 September 2011 */ public class FileServletRenderer implements ServletRenderer { private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB private static final String OCTECT_STREAM_MIME_TYPE = "application/octect-stream"; private String contentType = OCTECT_STREAM_MIME_TYPE; private long contentLength; private String contentDisposition = "inline"; private String fileName; private InputStream inputStream; /** * @return the contentType */ public String getContentType() { return contentType; } /** * @param contentType * the contentType to set */ public void setContentType(String contentType) { this.contentType = contentType; } /** * @return the contentLength */ public long getContentLength() { return contentLength; } /** * @param contentLength * the contentLength to set */ public void setContentLength(long contentLength) { this.contentLength = contentLength; } /** * @return the contentDisposition */ public String getContentDisposition() { return contentDisposition; } /** * @param contentDisposition * the contentDisposition to set */ public void setContentDisposition(String contentDisposition) { this.contentDisposition = contentDisposition; } /** * @return the fileName */ public String getFileName() { return fileName; } /** * @param fileName * the fileName to set */ public void setFileName(String fileName) { this.fileName = fileName; } /** * @return the inputStream */ public InputStream getInputStream() { return inputStream; } /** * @param inputStream * the inputStream to set */ public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public void setFile(File file) throws IOException { if (file == null) { throw new IOException("file is null."); } setInputStream(new BufferedInputStream(new FileInputStream(file))); setContentLength(file.length()); } /* * (non-Javadoc) * * @see org.bfs.bayweb.util.renderer.ServletViewRenderer#render(javax.servlet. * ServletRequest, javax.servlet.ServletResponse) */ public void render(ServletRequest request, ServletResponse response) throws IOException { // TODO Auto-generated method stub BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); try { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int inputStreamLength = 0; int length = 0; if (contentType == null) { contentType = request.getServletContext().getMimeType(getFileName()); } //We couldn't determine Content-Type if (contentType == null) { contentType = OCTECT_STREAM_MIME_TYPE; } while ((length = getInputStream().read(buffer)) > 0) { inputStreamLength += length; bos.write(buffer, 0, length); } if (inputStreamLength != getContentLength()) { setContentLength(inputStreamLength); } if (response instanceof HttpServletResponse) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.reset(); httpResponse.setHeader("Content-Type", getContentType()); httpResponse.setHeader("Content-Length", String.valueOf(getContentLength())); httpResponse.setHeader("Content-Disposition", "\"" + getContentDisposition() + "\"" + ((getFileName() != null && !getFileName().isEmpty()) ? "; filename=\"" + getFileName() + "\"" : "")); httpResponse.setHeader("Content-Type", getContentType()); } // finally bos.flush(); // clear } finally { // TODO Auto-generated catch block close(bos); close(getInputStream()); } } private void close(Closeable resource) throws IOException { if (resource != null) { resource.close(); } } }