如何用Golang实现文件上传功能_Golang 文件上传处理示例

文件上传需前端表单使用multipart/form-data编码;2. Golang后端通过ParseMultipartForm解析请求,用FormFile获取文件句柄并保存。

实现文件上传功能在Golang中非常直接,主要依赖标准库中的net/httpio包。通过解析HTTP请求中的multipart表单数据,可以读取并保存上传的文件。

1. 基础HTML上传表单

前端需要一个支持文件上传的表单,使用multipart/form-data编码类型:

2. 后端处理文件上传

在Golang服务端,使用http.Request.ParseMultipartForm()解析请求,并通过request.FormFile()获取文件句柄:

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
)

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
    }

    // 获取名为 uploadFile 的文件
    file, handler, err := r.FormFile("uploadFile")
    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)
}

func main() {
    // 确保上传目录存在
    os.MkdirAll("./uploads", os.ModePerm)

    http.HandleFunc("/upload", uploadHandler)
    http.Handle("/", http.FileServer(http.Dir("."))) // 提供静态页面

    fmt.Println("服务器启动在 :8080")
    http.ListenAndServe(":8080", nil)
}

3. 安全与优化建议

实际项目中需注意以下几点以提升安全性和稳定性:

  • 限制文件大小:通过ParseMultipartForm参数控制最大内存和磁盘缓存。
  • 验证文件类型:检查文件头或扩展名,防止恶意文件上传。
  • 重命名文件:避免覆盖或路径穿越攻击,使用UUID或时间戳命名。
  • 设置权限:确保uploads目录不可执行,防止上传脚本被运行。

4. 测试示例

将上述代码保存为main.go,在同一目录下创建index.html包含上传表单,然后运行:

go run main.go

访问 https://www./link/cbb686245ece57c9827c4bc0d0654a8e 即可测试上传功能。

基本上就这些。Golang的标准库已经足够支撑一个稳定、高效的文件上传服务,无需引入额外框架。关键在于正确处理错误和边界情况。