如何通过表单上传文件并让 Java 将其作为输入流处理?
我有一个 Java 类,它接受 InputStream 并对传入的数据进行编码。如何才能让某些东西能够接受用户上传的文件,但将其视为流,而不先将整个文件保存到磁盘并使用 FileInputStream?
基本上,我正在寻找一些 Java 中的轻量级 Web 框架,它可以让我获取用户作为数据流上传的文件。
I have a Java class that accepts an InputStream and encodes the data coming in. How do I get something working that will accept a file upload from the user, but treat it as a stream without saving the whole thing to disk first and using FileInputStream?
Basically I'm looking for some lightweight web framework in Java that will just let me get a file uploaded by the user as a data stream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果尚未完成,请安装 Web 服务器/servlet 容器,例如 Apache Tomcat。
如果您选择 Tomcat 6.x,则使用 Apache Commons FileUpload 获取上传的文件作为
输入流
。如果您选择 Tomcat 7.x (Servlet 3.0),则使用HttpServletRequest#getPart()
然后Part#getInputStream()
获取上传的文件作为InputStream< /代码>。
If not done yet, install a webserver/servletcontainer like Apache Tomcat.
If you pick Tomcat 6.x, then use Apache Commons FileUpload to get the uploaded file as an
InputStream
. If you pick Tomcat 7.x (Servlet 3.0), then useHttpServletRequest#getPart()
and thenPart#getInputStream()
to obtain the uploaded file as anInputStream
.该示例是从 Google App Engine 文档中提取的,因为 GAE 也不允许应用程序在磁盘上写入文件。原始示例可以在此处找到
The example is extracted from Google App Engine documents since GAE also doesn't allow the application to write files on disk. The original example can be found here