如何使用断点续传功能下载文件?

huangapple go评论99阅读模式
英文:

How to download a file with resume support?

问题

我正在使用Golang作为首选的编程工具。

我想要编写的程序是一个可以利用HTTP头中的范围请求来支持断点续传的程序,用于下载大文件,类似于aria2c

英文:

Am using golang as a programming tool of choice.

What i wanted to write is a program that downloads large files with resume support utilising range requests in the http headers
something similar to aria2c.

答案1

得分: 7

要做到这一点,你应该学习第14.16部分——Content-Range——RFC文档描述的HTTP/1.1协议,然后将这些知识应用于在执行客户端请求时操作发送的HTTP头集合——请参阅文档和其中的示例。

要计算请求的范围以继续下载,你应该获取当前文件的大小。可以通过调用打开的文件上的Stat()函数来找到这个大小——通过调用os.Open()返回的*os.File

你应该以追加模式打开文件,然后使用类似io.Copy()的方法将数据从Body HTTP响应成员流式传输到文件对象中。

自己研究如何在Go中从HTTP响应中读取数据——网络上有很多相关资源。

英文:

To do that you should study the part 14.16 — Content-Range — of the RFC document describing the HTTP/1.1 protocol and then apply that knowledge to manipulate the set of HTTP headers sent when the client request is executed — see the documentation and examples there.

To calculate the range to request, to continue downloading, you should get the current file's size. This can be found in the results returned by the Stat() function which can be called on an opened file — *os.File returned by a call to os.Open().

You should open your file in append mode and then use something like io.Copy() to stream the data from the Body HTTP response member to the file object.

Do your own research on how to read data from HTTP responses in Go — they are abundant on the Internet.

huangapple
  • 本文由 发表于 2014年1月18日 16:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/21201808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定