【发布时间】:2018-12-05 06:29:17
【问题描述】:
我正在使用 GO 以 10MB 的并发块下载一个大文件,如下所示。
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
func main() {
chunkSize := 1024 * 1024 * 10 // 10MB
url := "http://path/to/large/zip/file/zipfile.zip"
filepath := "zipfile.zip"
res, _ := http.Head(url)
maps := res.Header
length, _ := strconv.Atoi(maps["Content-Length"][0]) // Get the content length from the header request
// startByte and endByte determines the positions of the chunk that should be downloaded
var startByte = 0
var endByte = chunkSize - 1
for startByte < length {
if endByte > length {
endByte = length - 1
}
go func(startByte, endByte int) {
client := &http.Client {}
req, _ := http.NewRequest("GET", url, nil)
rangeHeader := fmt.Sprintf("bytes=%d-%d", startByte, endByte)
req.Header.Add("Range", rangeHeader)
resp,_ := client.Do(req)
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
addToFile(filepath, startByte, endByte, data)
}(startByte, endByte)
startByte = endByte + 1
endByte += chunkSize
}
}
func addToFile(filepath string, startByte, endByte int, data []byte) {
// TODO: write to byte range in file
}
我应该如何创建文件,并写入文件中与块的字节范围相对应的指定字节范围?
例如,如果我从字节 262144000-272629759 获取数据,则 addToFile 函数应写入 zipfile.zip 中的 262144000-272629759。然后,如果从另一个范围获取数据,则应将其写入 zipfile.zip 中的相应范围。
【问题讨论】:
-
旁白:绑定检查应该是
endByte >= length。