Golang实现一个简单的文件上传服务

正确处理 multipart/form-data 上传需先调用 r.ParseMultipartForm(32

http.HandleFunc 处理 multipart/form-data 上传

Go 标准库对文件上传支持很直接,关键不是自己解析 raw body,而是调用 r.ParseMultipartForm 触发解析,之后从 r.MultipartForm.File 拿到文件元信息,再用 file.Open() 获取可读流。

常见错误是忘记调用 ParseMultipartForm 就直接查 r.FormFile,结果返回 nil, http.ErrMissingFile;或者没设 MaxMemory 导致大文件直接写临时磁盘但没清理。

  • r.ParseMultipartForm(32 表示最多 32MB 在内存中,超量部分写临时文件(路径由 os.TempDir() 决定)
  • 必须在调用 r.FormFile 前执行 ParseMultipartForm,否则字段为空
  • r.FormFile("file") 返回的是 *multipart.FileHeader,不是文件内容本身

保存上传文件时注意 dst.Close() 和权限问题

os.Createos.OpenFile 创建目标文件后,必须显式 defer dst.Close(),否则文件句柄泄漏、Windows 下可能无法重复写入。另外 Go 默认创建的文件权限是 0644,Linux/macOS 下若服务以非 root 启动,需确保目标目录可写。

别直接拼接 filename 到路径里——用户传来的 ../../etc/passwd 会绕过校验。应该用 filepath.Base 截取纯文件名,或更稳妥地用 uuid.NewString() 重命名。

  • dst, err := os.Create(filepath.Join(uploadDir, safeName)) 创建文件
  • 务必 defer dst.Close(),且在 io.Copy 后检查 dst.Close() 的 error(尤其 NFS 或满盘时)
  • 上传前用 strings.HasSuffix(strings.ToLower(filename), ".jpg") 做简单扩展名校验,不能只信 Content-Type

http.ServeFile 提供静态文件下载(非必须但实用)

上传完想立刻能访问,最轻量方式是加个 GET 路由配 http.ServeFile。注意它不支持目录列表,请求路径必须精确匹配已存在的文件,否则 404;而且默认不设 Content-Disposition,浏览器可能内联显示而非下载。

如果上传目录是 ./uploads,那么 http.ServeFile(w, r, filepath.Join("./uploads", filename)) 是安全的,因为 filepath.Join 会自动清理路径穿越符号(.. 被归一化掉)。

  • 避免用 http.FileServer(http.Dir("./uploads")) 暴露整个目录——它允许 GET /..%2fetc%2fpasswd 这类编码绕过
  • 如需强制下载,手动设置 w.Header().Set("Content-Disposition", "attachment; filename="+filename)
  • 生产环境建议用 Nginx 静态服务,Go 进程只管上传逻辑
package main

import (
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

const uploadDir = "./uploads"

func init() {
	os.MkdirAll(uploadDir, 0755)
}

func uploadHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		http.ServeFile(w, r, "upload.html")
		return
	}

	if r.Method != "POST" {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	err := r.ParseMultipartForm(32 << 20)
	if err != nil {
		http.Error(w, "Unable to parse form", http.StatusBadRequest)
		return
	}

	file, header, err := r.FormFile("file")
	if err != nil {
		http.Error(w, "No file received", http.StatusBadRequest)
		return
	}
	defer file.Close()

	safeName := filepath.Base(header.Filename)
	if safeName == "" || strings.Contains(safeName, "..") {
		http.Error(w, "Invalid filename", http.StatusBadRequest)
		return
	}

	dst, err := os.Create(filepath.Join(uploadDir, safeName))
	if err != nil {
		http.Error(w, "Cannot create file", http.StatusInternalServerError)
		return
	}
	defer dst.Close()

	if _, err := io.Copy(dst, file); err != nil {
		http.Error(w, "Failed to save file", http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte("Upload OK: " + safeName))
}

func downloadHandler(w http.ResponseWriter, r *http.Request) {
	filename := filepath.Base(r.URL.Path[1:])
	if filename == "" {
		http.Error(w, "Bad request", http.StatusBadRequest)
		return
	}
	fpath := filepath.Join(uploadDir, filename)
	if _, err := os.Stat(fpath); os.IsNotExist(err) {
		http.Error(w, "File not found", http.StatusNotFound)
		return
	}
	http.ServeFile(w, r, fpath)
}

func main() {
	http.HandleFunc("/upload", uploadHandler)
	http.HandleFunc("/download/", downloadHandler)
	log.Println("Server starting on :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

最后提醒:这个实现没做并发限流、没校验文件头(Magic Number)、也没防重复上传。真实项目里,上传路径最好带时间戳或哈希前缀,避免同名覆盖;ParseMultipartForm 的内存限制值要根据实际带宽和服务器内存调整,不是越大越好。