Java多连接下载
我想得到一些建议,我已经开始一个新项目来创建一个将使用多个连接的 java 下载加速器。我想知道如何最好地解决这个问题。
到目前为止,我已经发现我可以使用 HttpUrlConnection 并使用 range 属性,但想知道一种有效的方法。一旦我从多个连接下载了各个部分,我就必须加入这些部分,以便我们最终得到一个完全下载的文件。
提前致谢 :)
I wanted to get some advice, I have started on a new project to create a java download accelerator that will use multiple connections. I wanted to know how best to go about this.
So far I have figured out that i can use HttpUrlConnection and use the range property, but wanted to know an efficient way of doing this. Once i have download the parts from the multiple connections i will then have to join the parts so that we end up with a fully downloaded file.
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
并将它们保存在不同的文件中:
myfile.part1
、myfile.part2
...我尝试使用以下代码来获取内容长度:
这是加入文件的示例代码:
and save them in different files:
myfile.part1
,myfile.part2
, …I tried the following code to get the content length:
Here's a sample code to join the files:
如果您想避免在下载后加入段,可以使用
文件通道
。使用
FileChannel
< /a>,您可以写入文件的任何位置(即使有多个线程)。因此,您可以首先分配整个文件,然后
将片段写在它们所属的位置在它们出现时。
有关详细信息,请参阅 Javadocs 页面 。
If you want to avoid joining segments after downloading you could use a
FileChannel
.With a
FileChannel
, you can write to any position of a file (even with multiple threads).So you could first allocate the whole file, and then
write the segments where they belong as they come in.
See the Javadocs page for more info.
JDownloader 是我见过的最好的下载器。如果您感兴趣,它是开源的,您当然可以从他们的代码中学到很多东西。
JDownloader is the best downloader I've seen. If you are interested, it's open source and surely you can learn a lot from their code.