dongshu4755
dongshu4755
2017-05-14 00:12

读取多部分请求会导致意外的EOF错误

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

Go version: 1.6.3 macos

I am trying to write an api to upload an apk file (several MB in most cases) to the server. Here is the client side code:

func syncApk(apkFile *os.File) {
    defer apkFile.Close()
    var buffer bytes.Buffer
    writer := multipart.NewWriter(&buffer)
    defer writer.Close()
    part, err := writer.CreateFormFile("apk", filepath.Base(apkFile.Name()))
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error creating form file: %v
", err)
        return
    }

    size, err := io.Copy(part, apkFile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error copying apk file data: %v
", err)
        return
    }
    fmt.Fprintf(os.Stdout, "Copied %v bytes for uploading...
", size)

    response, err := http.Post("http://localhost:8080/upload", writer.FormDataContentType(), &buffer)

    if err != nil {
        fmt.Fprintf(os.Stderr, "Error making POST request to sync apk: %v
", err)
        return
    }

    fmt.Fprintf(os.Stdout, "Successfully uploaded apk file: %v
", response.StatusCode)
}

Server code:

func main() {
    server := http.Server{
        Addr: ":8080",
    }
    http.HandleFunc("/upload", doApkUpload)
    server.ListenAndServe()
}

func doApkUpload(w http.ResponseWriter, r *http.Request) {
    file, _, err := r.FormFile("apk")
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error retrieving apk file: %v
", err)
        return
    }

    data, err := ioutil.ReadAll(file)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Error reading apk file content: %v", err)
        return
    }

    fmt.Fprintf(os.Stdout, string(data))
}

After I run the server on localhost I run client side code, I get:

Copied 1448401 bytes for uploading...
Successfully uploaded apk file: 200

It looks like the multipart file is written correctly. However I see this error on the server side:

Error retrieving apk file: unexpected EOF

Any idea where is the problem? Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqt66847 dqt66847 4年前

    The error indicates that the reader expected more data after the end of the request body. That missing data is the trailing boundary end line written by the multipart Close method.

    Call the Close method after writing all parts and before posting the form.

    func syncApk(apkFile *os.File) {
        defer apkFile.Close()
        var buffer bytes.Buffer
        writer := multipart.NewWriter(&buffer)
        part, err := writer.CreateFormFile("apk", filepath.Base(apkFile.Name()))
        if err != nil {
            fmt.Fprintf(os.Stderr, "Error creating form file: %v
    ", err)
            return
        }
    
        size, err := io.Copy(part, apkFile)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Error copying apk file data: %v
    ", err)
            return
        }
        fmt.Fprintf(os.Stdout, "Copied %v bytes for uploading...
    ", size)
        writer.Close()
        response, err := http.Post("http://localhost:8080/upload", writer.FormDataContentType(), &buffer)
    
        if err != nil {
            fmt.Fprintf(os.Stderr, "Error making POST request to sync apk: %v
    ", err)
            return
        }
        defer response.Body.Clse()
    
        fmt.Fprintf(os.Stdout, "Successfully uploaded apk file: %v
    ", response.StatusCode)
    }
    
    点赞 评论 复制链接分享

为你推荐