在Golang中实现文件上传与下载主要依赖net/http包,通过前端表单提交multipart/form-data数据,后端使用r.FormFile获取文件句柄并保存至服务器目录,同时设置ParseMultipartForm限制内存大小,再结合路由处理函数完成上传功能。
在Golang中实现文件上传与下载并不复杂,主要依赖标准库中的 net/http 包。通过合理组织路由和处理函数,可以轻松构建支持文件传输的Web服务。
文件上传(File Upload)
要实现文件上传,前端需要通过表单提交带有文件字段的请求,后端使用Go解析 multipart/form-data 数据。
1. 前端HTML表单示例:
2. 后端处理上传逻辑:
使用 r.FormFile("file") 获取上传的文件句柄,并将其保存到服务器指定目录。
func uploadHandler(whttp.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "只允许POST方法", http.StatusMethodNotAllowed) return } // 解析上传的文件(限制内存中最多10MB) err := r.ParseMultipartForm(10 << 20) if err != nil { http.Error(w, "解析表单失败", http.StatusBadRequest) return } file, handler, err := r.FormFile("file") if err != nil { http.Error(w, "获取文件失败", http.StatusBadRequest) return } defer file.Close() // 创建本地文件用于保存 dst, err := os.Create("./uploads/" + handler.Filename) if err != nil { http.Error(w, "创建文件失败", http.StatusInternalServerError) return } defer dst.Close() // 将上传的文件内容复制到本地文件 _, err = io.Copy(dst, file) if err != nil { http.Error(w, "保存文件失败", http.StatusInternalServerError) return } fmt.Fprintf(w, "文件 %s 上传成功", handler.Filename) }
文件下载(File Download)
文件下载的核心是设置正确的响应头,让浏览器触发下载行为,而不是直接显示内容。
实现步骤:
- 检查请求的文件是否存在
- 设置 Content-Disposition 头以提示下载
- 将文件内容写入响应体
func downloadHandler(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("file")
if filename == "" {
http.Error(w, "缺少文件名参数", http.StatusBadRequest)
return
}
filepath := "./uploads/" + filename
_, err := os.Stat(filepath)
if os.IsNotExist(err) {
http.Error(w, "文件不存在", http.StatusNotFound)
return
}
// 设置响应头,触发下载
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
w.Header().Set("Content-Type", "application/octet-stream")
// 读取并发送文件
http.ServeFile(w, r, filepath)
}
完整服务启动示例
将上传和下载处理器注册到HTTP路由,并确保上传目录存在。
func main() {
// 确保上传目录存在
os.MkdirAll("./uploads", os.ModePerm)
// 注册处理函数
http.HandleFunc("/upload", uploadHandler)
http.HandleFunc("/download", downloadHandler)
// 提供静态页面(可选)
http.Handle("/", http.FileServer(http.Dir("./static/")))
fmt.Println("服务器启动,端口: 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
基本上就这些。只要注意文件路径安全、大小限制和错误处理,就能实现一个基础但可靠的文件传输服务。实际项目中还可以加入身份验证、文件类型检查、防覆盖机制等增强功能。

http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "只允许POST方法", http.StatusMethodNotAllowed)
return
}
// 解析上传的文件(限制内存中最多10MB)
err := r.ParseMultipartForm(10 << 20)
if err != nil {
http.Error(w, "解析表单失败", http.StatusBadRequest)
return
}
file, handler, err := r.FormFile("file")
if err != nil {
http.Error(w, "获取文件失败", http.StatusBadRequest)
return
}
defer file.Close()
// 创建本地文件用于保存
dst, err := os.Create("./uploads/" + handler.Filename)
if err != nil {
http.Error(w, "创建文件失败", http.StatusInternalServerError)
return
}
defer dst.Close()
// 将上传的文件内容复制到本地文件
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, "保存文件失败", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "文件 %s 上传成功", handler.Filename)
}







