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

读取多部分请求会导致意外的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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站