下载和合并时输出文件损坏。

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

Output file is corrupted while downloading and merging

问题

大家好。

我正在尝试从Web服务器下载文件的部分并将其合并为一个文件。
以下是我的操作步骤:

out, err := os.Create(fileInfo.FileInfo.FileName)
	if err != nil {
		panic(err)
	}
	for _, hash := range fileInfo.FileInfo.FileBlocks {
		keeper := getKeeperByHash(hash.Hash, fileInfo.HasHash)
		log.Println("Downloading", hash.Hash)
		downloadURL := &url.URL{
			Scheme: "http",
			Host:   fmt.Sprintf("%s:%d", keeper.Ip, keeper.Port),
			Path:   "file/get",
		}
		param := url.Values{}
		param.Add("hash", hash.Hash)
		downloadURL.RawQuery = param.Encode()
		resp, err := http.Get(downloadURL.String())
		n, err := io.Copy(out, resp.Body)
		log.Println(n)
		if err != nil {
			panic(err)
		}
		resp.Body.Close()
	}
	out.Close()
	hash1, _ := GetFileHashAlt(fileInfo.FileInfo.FileName)
	if hash1 != fileInfo.FileInfo.FileHash {
		log.Println(hash1)
		log.Println(fileInfo.FileInfo.FileHash)

		panic("HASH NOT THE SAME")
	}
	log.Println(hash1)
	log.Println(fileInfo.FileInfo.FileHash)

但是对于二进制文件,哈希值不同,文件损坏了。对于文本文件,一切正常。
问题可能出在哪里?
当我上传和拆分文件时,一直在检查哈希值,而且始终相同。

我尝试先下载文件再拆分并合并为一个文件,但问题仍然存在。

英文:

everyone.

I'm try to download file parts from web server and merge it into a single file.
Here's how i did it:

out, err := os.Create(fileInfo.FileInfo.FileName)
	if err != nil {
		panic(err)
	}
	for _, hash := range fileInfo.FileInfo.FileBlocks {
		keeper := getKeeperByHash(hash.Hash, fileInfo.HasHash)
		log.Println("Downloading", hash.Hash)
		downloadURL := &url.URL{
			Scheme: "http",
			Host:   fmt.Sprintf("%s:%d", keeper.Ip, keeper.Port),
			Path:   "file/get",
		}
		param := url.Values{}
		param.Add("hash", hash.Hash)
		downloadURL.RawQuery = param.Encode()
		resp, err := http.Get(downloadURL.String())
		n, err := io.Copy(out, resp.Body)
		log.Println(n)
		if err != nil {
			panic(err)
		}
		resp.Body.Close()
	}
	out.Close()
	hash1, _ := GetFileHashAlt(fileInfo.FileInfo.FileName)
	if hash1 != fileInfo.FileInfo.FileHash {
		log.Println(hash1)
		log.Println(fileInfo.FileInfo.FileHash)

		panic("HASH NOT THE SAME")
	}
	log.Println(hash1)
	log.Println(fileInfo.FileInfo.FileHash)

But for the binary files hashes is not the same and file is corrupted. For text files everything is ok.
Where can be the problem?
When i',m uploading and splitting file, i'm checking hashes all the way and it's always the same.

I tried first split files after download and them merge it to a one file, but the problem is still there.

答案1

得分: 0

问题出在服务器端。Fastify框架会改变文件部分的内容。将其替换为Express JS解决了这个问题。

英文:

The problem was at the server side. Fastify framework was changing file part contents. Replacing it to Express JS solved the problem.

huangapple
  • 本文由 发表于 2023年5月6日 18:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188343.html
匿名

发表评论

匿名网友

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

确定