如何从后端Java代码发送PDF文件?
我有一个 JSP 文件,有后端帮助程序类。我需要从后端助手将 PDF 文件作为附件发送到 JSP。我怎样才能做到这一点?
I have a JSP file, there backend helper class to it. From the back end helper I need to send PDF file to the JSP as an attachment. How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 Apache Commons File Upload 组件。这可能是最好的方法,而不是重新发明轮子。 ;)
I would suggest you to use Apache Commons File Upload component. That's probably the best way rather than reinventing the wheel. ;)
由于您没有告诉我们您是否使用任何 MVC 框架或只是简单的 Servlet,因此我将介绍基础知识。
如果您想上传文件,Apache Commons File Upload 是最好的库,它将翻译您的
multipart/form-data
编码的HttpServletRequest
消息,并为您提供上传的文件(在InputStream
格式,大多是首选)。作为开发人员,您可以将数据写回到您选择的持久存储中。
相反,它获取文件,获取适当的 MIME-Type、Content-Length(文件大小)和文件数据(
InputStream
,如果可能)并将其渲染回 HttpServletResponse )。这段代码(功能齐全,由我编写)确实将文件作为附件/内联。
最重要的方法是
render(HttpServletRequest, HttpServletResponse)
。Since you haven't told us if you're using any MVC framework or just plain Servlet, I'll go for the basics.
If you want to upload a file, Apache Commons File Upload is the best library that will translate your
multipart/form-data
encodedHttpServletRequest
message and provide you with files uploaded (inInputStream
format, mostly preferred).It's up to you, the developer, to write the data back to a persistent storage of your choice.
The reverse, it's to take the file, get the appropriate MIME-Type, Content-Length (file size), and file data (
InputStream
, if possible) and render it back to theHttpServletResponse
).This code (fully functional and written by me) does put the file as attachment/inline.
The most important method is
render(HttpServletRequest, HttpServletResponse)
.