首先实现文件上传功能,通过http.Request.ParseMultipartForm解析表单,调用request.FormFile获取文件句柄,并用io.Copy将文件保存至服务器路径,同时限制文件大小和类型以防止恶意上传。

在Go语言(Golang)开发中,实现文件的上传与下载是Web服务中的常见需求,比如用户头像上传、附件下载等场景。本文将从基础原理到完整示例,带你一步步实现一个简单但实用的文件上传下载服务。
文件上传通常通过HTTP的multipart/form-data格式提交。Golang的net/http包原生支持解析这种请求。
关键步骤:
http.Request.ParseMultipartForm()解析表单数据request.FormFile()获取上传的文件句柄io.Copy()将文件内容保存到服务器指定路径示例代码:
立即学习“go语言免费学习笔记(深入)”;
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "只允许POST请求", http.StatusMethodNotAllowed)
return
}
// 解析 multipart 表单,最大内存 32MB
err := r.ParseMultipartForm(32 << 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)
}
文件下载的核心是设置正确的响应头,告诉浏览器这是一个需要下载的文件,而不是直接展示。
关键点:
os.Open()打开服务器上的文件Content-Disposition头触发下载Content-Type为application/octet-stream表示二进制流http.ServeFile()或io.Copy()发送文件内容示例代码:
立即学习“go语言免费学习笔记(深入)”;
func downloadHandler(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("name")
if filename == "" {
http.Error(w, "缺少文件名", http.StatusBadRequest)
return
}
filepath := "./uploads/" + filename
// 检查文件是否存在
if _, err := os.Stat(filepath); 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)
}
在生产环境中,必须考虑安全性和稳定性。
../../etc/passwd。可使用path.Base()提取文件名ParseMultipartForm中设置合理上限一个简单的项目目录结构:
/fileserver ├── main.go ├── uploads/ ├── static/ │ └── upload.html └── go.mod
main.go 路由注册:
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/upload", uploadHandler)
mux.HandleFunc("/download", downloadHandler)
mux.Handle("/", http.FileServer(http.Dir("./static/")))
fmt.Println("服务启动在 :8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}
配合一个简单的HTML页面即可测试上传功能。
基本上就这些。掌握以上内容后,你可以在此基础上扩展出支持分片上传、断点续传、权限验证等功能。核心在于理解HTTP文件传输机制和Go的标准库能力。以上就是如何在Golang中开发文件上传下载功能_Golang文件上传下载项目实战汇总的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号