问题描述
我正在尝试允许我的 Java 服务器传输一个文件,以便网络浏览器可以下载.
I am trying to allow my java server to transfer a file where a web browser can download.
但是,如果浏览器暂停并恢复文件传输工作并且不再重新开始下载,我希望浏览器能够正常工作.
However, I want the browser if they pause and resume the file transfer to work and not start the download all over again.
有人知道服务器端的java代码来解决这个问题吗?
Anyone know java code for the server side to fix this issue?
推荐答案
支持可恢复下载有两个部分:
There are two parts to supporting resumable downloads:
- 您的回复需要返回接受范围 和 Content-Range 标头
- 您需要编写可以处理 HTTP 范围的代码 标头和 If-Range 要知道客户需要恢复的地方
- Your response needs to return the Accept-Ranges and Content-Range headers
- You need to write code that can then handle the HTTP Range headers and If-Range to know where a client needs to resume
如果您提供静态资源,最好的办法是使用代理服务器(如 Apache)来处理下载.如果代理服务器不是一个选项,那么您可能会找到与您的应用程序服务器捆绑在一起的 Servlet;例如,Tomcat 具有 DefaultServlet.此选项的缺点是它会在您的应用程序和应用程序服务器之间创建一个硬依赖关系,这可能是不可接受的.
If you're serving up a static resource your best bet is to use a proxy server, like Apache, to handle the download. If a proxy server isn't an option then you can probably find a Servlet that is bundled with your app server; for example, Tomcat has as DefaultServlet. The downside to this option is that it creates a hard dependency between your application and the application server, which may not be acceptable.
如果您决定自己动手,我建议您查看 Apache 的 HTTP 组件.这是一个很好的 API,可以更轻松地处理 HTTP 请求和响应.
If you decide to roll your own I recommend taking a look at Apache's HTTP Components. It's a nice API that makes working with HTTP requests and responses much easier.
这篇关于Java服务器端发送带有简历支持的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!