如何使用 Java 分块上传大文件?
我需要使用 Java 分块上传一个大文件。
有没有示例代码可以参考?
I need to upload a large file in chunks using Java.
Is there any sample code I can refer to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
*可以使用 plupload 来完成。
这是样本。
我的index.html如下:-
我的java后端代码(Servlet)如下:-
请参阅plupload了解详细信息,在github上您可以看到jakobadam和rocky的示例项目。
如果需要上传多个文件,请告诉我。使用 plupload 我们可以上传任意数量、任意大小的文件。此示例适用于非常大的单个文件上传。
不要忘记包含 plupload.full.min.js。希望这有帮助*强调文字**
*It can be done using plupload.
Here is sample.
My index.html is as under:-
My java backend code(Servlet) is as under:-
Please refer to plupload for details and on github you can see sample projects by jakobadam and rocky.
Please let me know if multiple file upload is required. Using plupload we can upload any number of files of any sizes. This sample is for single file upload of very large size.
dont forget to include plupload.full.min.js. Hope this helps*emphasized text**
以下是使用块上传文件的本机 Java 代码示例:
此示例中的代码基于此答案从java上传http文件。区别在于对
connection.setChunkedStreamingMode(4096);
的调用定义了应使用分块流。Here is an example of native Java code that uploads a file using chunks:
The code in this example is based on this answer about http file upload from java. The difference is the call to
connection.setChunkedStreamingMode(4096);
that defines that chunked streaming should be used.尝试 Apache Commons 上传。它支持流媒体,可能适合您。
Try Apache Commons upload. It supports streaming, may be suitable for you.
您可以简单地自己分解文件,使用 Socket API,并重新组装文件。
You can simply break the file up yourself, send it using the Socket API, and re-assemble the file.
使用随机访问文件。我相信这已经在 SO 上进行了介绍。
具有 rewind()/reset() 功能的 java 文件输入
只需寻找起点,从那里写入您想要的任意字节,并记住您停止写入的点。
Use a RandomAccessFile. This has already been covered on SO I believe.
java file input with rewind()/reset() capability
Basically you'd just seek to the starting point, write however many bytes you want to from there, and remember the point you stopped writing from.