需要使用 java 线程下载文件部分的帮助

发布于 2025-01-05 02:06:25 字数 172 浏览 5 评论 0 原文

我正在尝试从服务器下载用户指定的部分数(n)的文件。因此,有一个 x 字节的文件,分为 n 个部分,每个部分同时下载整个文件的一部分。我正在使用线程来实现这一点,但我之前没有使用过 http,并且不太了解下载文件的真正工作原理。我已经阅读过它,似乎需要使用“范围”,但我不知道如何下载不同的部分并能够在不损坏数据的情况下附加它们。

I am trying to download a file from a server in a user specified number of parts (n). So there is a file of x bytes divided into n parts with each part downloading a piece of the whole file at the same time. I am using threads to implement this, but I have not worked with http before and do not really understand how downloading a file really works. I have read up on it and it seems "Range" needs to be used, but I do not know how to download different parts and being able to append them without corrupting the data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹂绝世的画 2025-01-12 02:06:25

(因为这是一项家庭作业,我只会给您一个提示)

附加到单个文件对您没有任何帮助,因为这会弄乱数据。您有两种选择:

  • 从每个线程下载到单独的临时文件,然后按正确的顺序合并临时文件以创建最终文件。这可能更容易想象,但却是一种相当丑陋且低效的方法。

  • 不要坚持通常的流式语义 - 使用随机访问(12 >) 写入数据从每个线程直接到输出文件中的正确位置。

(Since it's a homework assignment I will only give you a hint)

Appending to a single file will not help you at all, since this will mess up the data. You have two alternatives:

  • Download from each thread to a separate temporary file and then merge the temporary files in the right order to create the final file. This is probably easier to conceive, but a rather ugly and inefficient approach.

  • Do not stick to the usual stream-style semantics - use random access (1, 2) to write data from each thread straight to the right location within the output file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文