Go新手如何做一个文件上传服务_Go上传功能实战

5次阅读

http请求体r.Body是单次可读流,未缓冲;若中间件等提前读取或解析(如ParseForm/ParseMultipartForm),handler中再读将得空或io.EOF;ParseMultipartForm需指定内存上限,大文件需合理设置。

Go新手如何做一个文件上传服务_Go上传功能实战

为什么 http.HandleFunc 里直接读 r.Body 会失败?

因为 go 的 HTTP 请求体(r.Body)是单次可读的流,且默认未做缓冲。如果中间件、日志或框架提前调用过 r.Body.Read()(比如解析了表单),再在 handler 里读就会返回空或 io.EOF

  • 上传文件必须用 r.ParseMultipartForm()r.ParseForm() 预解析,否则 multipart.Reader 拿不到字段
  • r.FormValue("xxx") 只对已解析的表单有效;没调 ParseXxx 就调,值为空
  • 大文件上传时,ParseMultipartForm(32 的参数是内存上限(这里是 32MB),超限会报 http: request body too large

如何用 req.MultipartReader() 安全读取文件流?

绕过自动解析、手动控制流,适合大文件或需要校验/转换的场景。关键点是:不调 ParseMultipartForm,直接从 r.Body 构造 multipart.Reader,再逐 part 处理。

  • 先检查 r.Header.Get("Content-Type") 是否以 multipart/form-data 开头,否则拒绝
  • multipart.NewReader(r.Body, boundary),其中 boundary 要从 Content-Type 头里提取,不能硬编码
  • 每个 partHeader.Get("Content-Disposition") 必须含 filename= 才视为文件,否则可能是普通字段
  • 务必用 io.CopyN(dst, part, maxFileSize) 限制写入大小,防止磁盘打满

保存文件前必须检查的三件事

跳过校验直接 os.Create 是生产事故高发区。

  • 文件名要 sanitize:filepath.Base(filename) 去掉路径遍历(如 ../../etc/passwd
  • 扩展名要白名单校验:strings.HasSuffix(strings.ToLower(name), ".jpg"),禁用 .php.js 等可执行后缀
  • 目标目录需预先存在且可写:os.MkdirAll(uploadDir, 0755),否则 os.Createno such file or directory

完整可运行的上传 handler 示例

这个示例支持单文件上传、基础校验、流式保存,无第三方依赖:

func uploadHandler(w http.ResponseWriter, r *http.Request) { 	if r.Method != "POST" { 		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) 		return 	}  	// 检查 Content-Type 	ct := r.Header.Get("Content-Type") 	if !strings.HasPrefix(ct, "multipart/form-data") { 		http.Error(w, "Bad Content-Type", http.StatusBadRequest) 		return 	}  	// 提取 boundary 	boundary, _ := mime.ParseMediaType(ct) 	if boundary == "" { 		http.Error(w, "No boundary in Content-Type", http.StatusBadRequest) 		return 	}  	mr, err := multipart.NewReader(r.Body, boundary["boundary"]) 	if err != nil { 		http.Error(w, "Invalid multipart body", http.StatusBadRequest) 		return 	}  	const maxFileSize = 10 << 20 // 10MB 	for { 		part, err := mr.NextPart() 		if err == io.EOF { 			break 		} 		if err != nil { 			http.Error(w, "Read part failed", http.StatusInternalServerError) 			return 		}  		filename := part.FileName() 		if filename == "" { 			continue // skip non-file fields 		}  		// Sanitize filename 		cleanName := filepath.Base(filename) 		if cleanName == "." || cleanName == ".." { 			http.Error(w, "Invalid filename", http.StatusBadRequest) 			return 		}  		// Check extension 		ext := strings.ToLower(filepath.Ext(cleanName)) 		switch ext { 		case ".jpg", ".jpeg", ".png", ".gif": 			// ok 		default: 			http.Error(w, "Unsupported file type", http.StatusBadRequest) 			return 		}  		// Open output file 		out, err := os.Create(filepath.Join("./uploads", cleanName)) 		if err != nil { 			http.Error(w, "Cannot create file", http.StatusInternalServerError) 			return 		}  		// Copy with size limit 		_, err = io.CopyN(out, part, maxFileSize+1) 		out.Close() 		if err == io.EOF || err == nil { 			// success 			w.WriteHeader(http.StatusOK) 			w.Write([]byte("uploaded: " + cleanName)) 			return 		} else if err == io.ErrShortWrite { 			http.Error(w, "File too large", http.StatusBadRequest) 			os.Remove(filepath.Join("./uploads", cleanName)) 			return 		} else { 			os.Remove(filepath.Join("./uploads", cleanName)) 			http.Error(w, "Write error", http.StatusInternalServerError) 			return 		} 	} 	http.Error(w, "No file found", http.StatusBadRequest) }

注意 io.CopyN 返回 io.ErrShortWrite 表示写满上限,此时必须删掉已创建的空文件,否则残留垃圾文件。

text=ZqhQzanResources