使用Golang构建文件上传服务需先调用r.ParseMultipartForm(maxMemory)解析请求,再通过r.MultipartForm.File获取文件句柄;maxMemory建议设为32MB,否则r.MultipartForm为nil。
使用 Golang 构建文件上传服务,核心在于正确解析 multipart/form-data 请求、校验文件安全性、并可靠地保存到本地或外部存储。整个过程不复杂,但几个关键点容易出错:表单字段顺序、文件大小限制、文件名处理、MIME 类型验证和错误响应设计。
Go 标准库 net/http 原生支持 multipart 解析,无需第三方包。关键步骤是调用 r.ParseMultipartForm() 预分配内存缓冲,再通过 r.MultipartForm.File 获取文件句柄:
r.ParseMultipartForm(maxMemory),否则 r.MultipartForm 为 nil;maxMemory 建议设为 32
formFile("file") 获取单个文件(推荐),它内部已处理边界检查和打开操作;若需多个同名字段,用 form.File["file"] 遍历err —— 常见错误包括 http.ErrMissingFile(前端未传 file 字段)、http.ErrNotMultipart(Content-Type 不匹配)仅依赖前端传来的文件名和 MIME 类型不可信,需服务端双重校验:
*multipart.FileHeader 中提取原始文件名时,用 path.Base() 截取,避免路径遍历(如 ../../etc/passwd)f.Open() 后 io.ReadFull(header, buf)),用 http.DetectContentType(buf) 推测真实 MIME 类型,与 header.Header.Get("Content-Type") 比对.jpg, .png, .pdf),但不要仅靠后缀判断——应结合 MIME 和 magic bytes 校验http.Server 中配置 MaxRequestBodySize,或在 handler 开头用 r.ContentLength 快速拒绝超大请求保存阶段需兼顾原子性、可读性和清理机制:
uuid.New().String() + ext),避免覆盖和冲突;原名可存入数据库或文件元数据中os.CreateTemp(dir, "upload-*.tmp") 创建临时文件,完整写入后再 os.Rename() 覆盖目标路径,确保写入过程不被意外读取minio-go 或 aws-sdk-go-v2 的 PutObject 方法,直接流式上传(io.Copy(uploader, file)),避免本地落盘以下是一个生产可用的最小可行 handler 片段(省略 import 和 server 启动):
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// 解析 multipart 表单,最大内存 32MB
if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, "Invalid request payload", http.StatusBadRequest)
return
}
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "No file uploaded or invalid field name", http.StatusBadRequest)
return
}
defer file.Close()
// 安全校验文件名
filename := path.Base(header.Filename)
if filename == "." || filename == "" {
http.Error(w, "Invalid filename", http.StatusBadRequest)
return
}
// 检查扩展名(示例)
ext := strings.ToLower(path.Ext(filename))
allowedExts := map[string]bool{".jpg": true, ".png": true, ".pdf": true}
if !allowedExts[ext] {
http.Error(w, "File type not allowed", http.StatusUnprocessableEntity)
return
}
// 生成唯一路径
dstPath := filepath.Join("./uploads", uuid.New().String()+ext)
// 原子写入
outFile, err := os.CreateTemp("./uploads", "upload-*.tmp")
if err != nil {
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
return
}
defer os.Remove(outFile.Name()) // 清理临时文件
if _, err := io.Copy(outFile, file); err != nil {
http.Error(w, "Failed to save file", http.StatusInternalServerError)
return
}
outFile.Close()
if err := os.Rename(outFile.Name(), dstPath); err != nil {
http.Error(w, "Failed to finalize file", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"filename": filename,
"url": "/uploads/" + filepath.Base(dstPath),
"size": header.Size,
})
}