在Golang中处理HTTP上传的Zip文件

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

Handle Http Upload Zip file in Golang

问题

我正在使用golang的net/http包通过Postman获取上传的zip文件。
附件文件链接。这不是危险文件,请随意查看。

开发环境

  • 本地机器 m1 macbook pro golang 1.17.2 - 没有问题
  • 服务器 Docker 镜像 golang:1.17.5-stretch - 出现问题

以下是捕获 post 表单中的 transSourceFile 文件的代码。

  1. func HandleFileReqTest(w http.ResponseWriter, req *http.Request, params map[string]string) err {
  2. if err := req.ParseMultipartForm(32 << 20); err != nil {
  3. return err
  4. }
  5. file, header, err := req.FormFile("transSourceFile")
  6. if err != nil {
  7. return err
  8. }
  9. defer file.Close()
  10. fmt.Println("header.Size:", header.Size)
  11. return nil
  12. }

我也尝试了下面的代码,但没有用。

  1. func HandleFileReqTest(w http.ResponseWriter, req *http.Request, params map[string]string) err {
  2. if err := req.ParseForm(); err != nil {
  3. return err
  4. }
  5. req.ParseMultipartForm(32 << 20)
  6. file, header, err := req.FormFile("transSourceFile")
  7. if err != nil {
  8. return err
  9. }
  10. defer file.Close()
  11. fmt.Println("header.Size:", header.Size)
  12. return nil
  13. }

结果:
本地机器得到的文件大小与原始文件相同。
使用 golang:1.17.5-stretch 的服务器得到的文件大小与原始文件不同。

因此,我无法在服务器上解压文件。有人可以帮忙吗?

英文:

I'm using golang net/http package to retrieve the uploaded zip file via postman.
The attachment file link. It is not dangerous file. Feel free to check out.

Development env

  • local machine m1 macbook pro golang 1.17.2 - no issue
  • server docker image golang:1.17.5-stretch - got issue.

Code to capture the post form transSourceFile file.

  1. func HandleFileReqTest(w http.ResponseWriter, req *http.Request, params map[string]string) err {
  2. if err := req.ParseMultipartForm(32 &lt;&lt; 20); err != nil {
  3. return err
  4. }
  5. file, header, err := req.FormFile(&quot;transSourceFile&quot;)
  6. if err != nil {
  7. return err
  8. }
  9. defer file.Close()
  10. fmt.Println(&quot;header.Size:&quot;, header.Size)
  11. return nil
  12. }

I tried below code also no use

  1. func HandleFileReqTest(w http.ResponseWriter, req *http.Request, params map[string]string) err {
  2. if err := req.ParseForm(); err != nil {
  3. return err
  4. }
  5. req.ParseMultipartForm(32 &lt;&lt; 20)
  6. file, header, err := req.FormFile(&quot;transSourceFile&quot;)
  7. if err != nil {
  8. return err
  9. }
  10. defer file.Close()
  11. fmt.Println(&quot;header.Size:&quot;, header.Size)
  12. return nil
  13. }

Result:
Local machine got the same file size as the origin file.
Server with golang:1.17.5-stretch got the different file size compare to origin file.

As the result on this, i'm unable to unzip the file in the server. Anyone can help?

答案1

得分: 2

你需要将文件从一个位置复制到另一个位置:

  1. f, err := os.Create("some.zip")
  2. defer f.Close()
  3. n, err := io.Copy(f, file)
英文:

You need to copy form file to the actual file:

  1. f, err := os.Create(&quot;some.zip&quot;)
  2. defer f.Close()
  3. n, err := io.Copy(f, file)

答案2

得分: 0

数据没有完全刷新到文件中。您应该先关闭文件以确保数据完全刷新。

// 创建一个本地文件 filename
dst, err := os.Create("filename.zip")

// 保存文件
fl, err = io.Copy(dst, src)

// 关闭文件
dst.Close()

stat, _ := dst.Stat()

// 现在在刷新文件后检查大小 stat.Size() 或 header.Size。

英文:

Data isn't being flushed to the file completely. You should close the file first to ensure that the data is fully flushed.

  1. // create a local file filename
  2. dst, err := os.Create(&quot;filename.zip&quot;)
  3. // save it
  4. fl, err = io.Copy(dst, src)
  5. // Close the file
  6. dst.Close()
  7. stat, _ := dst.Stat()
  8. //Now check the size stat.Size() or header.Size after flushing the file.

huangapple
  • 本文由 发表于 2022年1月13日 11:08:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/70690970.html
匿名

发表评论

匿名网友

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

确定