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

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

Reading multipart request results in Unexpected EOF error

问题

Go版本:1.6.3 macOS

我正在尝试编写一个API来将一个APK文件(大多数情况下几MB大小)上传到服务器。以下是客户端的代码:

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, "创建表单文件时出错:%v\n", err)
        return
    }

    size, err := io.Copy(part, apkFile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "复制APK文件数据时出错:%v\n", err)
        return
    }
    fmt.Fprintf(os.Stdout, "已复制 %v 字节用于上传...\n", size)

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

    if err != nil {
        fmt.Fprintf(os.Stderr, "向服务器发起POST请求同步APK时出错:%v\n", err)
        return
    }

    fmt.Fprintf(os.Stdout, "成功上传APK文件:%v\n", response.StatusCode)
}

服务器端的代码:

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, "获取APK文件时出错:%v\n", err)
        return
    }

    data, err := ioutil.ReadAll(file)
    if err != nil {
        fmt.Fprintf(os.Stderr, "读取APK文件内容时出错:%v", err)
        return
    }

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

在本地主机上运行服务器后,运行客户端代码,我得到以下输出:

已复制 1448401 字节用于上传...
成功上传APK文件:200

看起来多部分文件已经正确写入。然而,在服务器端我看到以下错误:

获取APK文件时出错:unexpected 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\n", err)
		return
	}

	size, err := io.Copy(part, apkFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error copying apk file data: %v\n", err)
		return
	}
	fmt.Fprintf(os.Stdout, "Copied %v bytes for uploading...\n", 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\n", err)
		return
	}

	fmt.Fprintf(os.Stdout, "Successfully uploaded apk file: %v\n", 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\n", 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

得分: 9

错误提示表示读取器在请求体结束后期望更多的数据。缺失的数据是由multipart Close方法写入的尾部边界结束行。

在写入所有部分并在提交表单之前调用Close方法。

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, "创建表单文件时出错:%v\n", err)
        return
    }

    size, err := io.Copy(part, apkFile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "复制apk文件数据时出错:%v\n", err)
        return
    }
    fmt.Fprintf(os.Stdout, "已复制 %v 字节以进行上传...\n", size)
    writer.Close()
    response, err := http.Post("http://localhost:8080/upload", writer.FormDataContentType(), &buffer)

    if err != nil {
        fmt.Fprintf(os.Stderr, "同步apk时发出POST请求时出错:%v\n", err)
        return
    }
    defer response.Body.Close()

    fmt.Fprintf(os.Stdout, "成功上传apk文件:%v\n", response.StatusCode)
}
英文:

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\n", err)
		return
	}

	size, err := io.Copy(part, apkFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error copying apk file data: %v\n", err)
		return
	}
	fmt.Fprintf(os.Stdout, "Copied %v bytes for uploading...\n", 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\n", err)
		return
	}
    defer response.Body.Clse()

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

huangapple
  • 本文由 发表于 2017年5月14日 08:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/43959180.html
匿名

发表评论

匿名网友

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

确定