dongshu4755
2017-05-14 00:12
浏览 261
已采纳

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

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 2017-05-14 01:55
    已采纳

    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)
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题