我正在尝试从服务器下载用户指定的部分数(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.
发布评论
评论(1)
(因为这是一项家庭作业,我只会给您一个提示)
附加到单个文件对您没有任何帮助,因为这会弄乱数据。您有两种选择:
从每个线程下载到单独的临时文件,然后按正确的顺序合并临时文件以创建最终文件。这可能更容易想象,但却是一种相当丑陋且低效的方法。
不要坚持通常的流式语义 - 使用随机访问(1,2 >) 写入数据从每个线程直接到输出文件中的正确位置。
(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.