从运行 Java Jersey Rest API 的 HTTP 服务器传输大文件
我正在使用 Java JDK 1.7 和 Jersey Web 服务框架编写一个 Web 服务.我需要提供的其中一件事是允许经过身份验证的客户端下载某些大型数据文件(1-3 GB).理想情况下,我希望这是一个暂停和恢复类型的可下载选项.我尝试了 jersey 多部分 API,并且能够让它在我的客户端机器上工作,最大 400 MB,但除此之外它遇到了内存不足的问题.我还担心服务器在面临同时下载请求时可能会失败.关于如何做到这一点的任何想法?Netty 是一种选择吗?关于如何将 Netty 集成到现有的基于 Jersey 的 Web 服务中的任何指示?是否有其他框架作品可以帮助实现这一目标?我确实必须将 java 用于 Web 服务.任何指针都会有所帮助.
I am writing a web service using Java JDK 1.7 and Jersey Web service Framework. One of the things I need to provide is a way to allow authenticated clients to download certain large data files ( 1-3 GB). Ideally I would like this to be a pause and resume type downloadable option. I tried the jersey multi-part API and was able to get it to work on my client machine upto 400 MB but beyond that it ran into out-of memory issues. I am also worried that the server might fail when faced with simultaneous download requests. Any thoughts on how this can be done? Is Netty an option? Any pointers on how Netty can be integrated into a existing Jersey based web service? Are there other frame works available to help accomplish this? I do have to use java for the web service. Any pointers will be helpful.
推荐答案
如果您遇到内存不足的问题,您应该检查您是如何处理正在下载的数据的.如果您使用 Jersey 的 ClientResponse,请确保您使用的是 getEntityInputStream() 而不是 getEntity().这样,您可以流式传输数据,将其写入文件,然后将其丢弃,而不是让它在 Java 堆空间中堆积.
If you are getting stuck on out-of-memory issues, you should check how you are handling the data you are downloading. If you are using Jersey's ClientResponse, make sure you are using getEntityInputStream() and not getEntity(). This way, you can stream the data, write it to file, and toss it aside, rather than letting it build up in the Java heap space.
我无法真正谈论您的同时下载问题,但如果您使用的是 Web 服务框架,那么应该妥善处理.
I can't really speak about your simultaneous download concerns, but if you are using the web services framework, then it should be handled properly.
对于这两个问题,有关您的具体实现的更多信息,尤其是代码,将帮助您获得更好的响应.
For both issues, more info on your specific implementation, especially code, will help you get a better response.
相关文章