[golang]简单文件上传服务
来源:互联网 发布:linux 安装eclipse 编辑:程序博客网 时间:2024/05/21 15:34
利用net/http库及gorilla/mux库实现了一个简单的文件上传服务,
示例如下:
package mainimport ( "fmt" "github.com/gorilla/mux" "io" "net/http" "os")const uploadHTML = `<html> <head> <title>选择文件</title> </head> <body> <form enctype="multipart/form-data" action="/" method="post"> <input type="file" name="uploadfile" /> <input type="submit" value="上传文件" /> </form> </body> </html>`const destLocalPath = "/data/files/"func index(w http.ResponseWriter, r *http.Request) { w.Write([]byte(uploadHTML))}func upload(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { index(w, r) return } r.ParseMultipartForm(32 << 20) // max memory is set to 32MB clientfd, handler, err := r.FormFile("uploadfile") if err != nil { fmt.Println(err) w.Write([]byte("upload failed.")) return } defer clientfd.Close() localpath := fmt.Sprintf("%s%s", destLocalPath, handler.Filename) localfd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println(err) w.Write([]byte("upload failed.")) return } defer localfd.Close() io.Copy(localfd, clientfd) w.Write([]byte("upload finish."))}func newRouter() http.Handler { hdl := mux.NewRouter() hdl.HandleFunc("/", upload) return hdl}func main() { http.ListenAndServe(":8877", newRouter())}
假如需要在接收文件的时候计算文件hash值, 应该如何做呢?
根据io.TeeReader库,可以在文件上传过程中自动计算hash值, 完整代码修改为:
package mainimport ( "crypto/sha1" "encoding/hex" "fmt" "github.com/gorilla/mux" "io" "net/http" "os")const uploadHTML = `<html> <head> <title>选择文件</title> </head> <body> <form enctype="multipart/form-data" action="/" method="post"> <input type="file" name="uploadfile" /> <input type="submit" value="上传文件" /> </form> </body> </html>`const destLocalPath = "/data/files/"func index(w http.ResponseWriter, r *http.Request) { w.Write([]byte(uploadHTML))}func upload(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { index(w, r) return } r.ParseMultipartForm(32 << 20) // max memory is set to 32MB clientfd, handler, err := r.FormFile("uploadfile") if err != nil { fmt.Println(err) w.Write([]byte("upload failed.")) return } defer clientfd.Close() localpath := fmt.Sprintf("%s%s", destLocalPath, handler.Filename) localfd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println(err) w.Write([]byte("upload failed.")) return } defer localfd.Close() // 利用io.TeeReader在读取文件内容时计算hash值 fhash := sha1.New() io.Copy(localfd, io.TeeReader(clientfd, fhash)) hstr := hex.EncodeToString(fhash.Sum(nil)) w.Write([]byte(fmt.Sprintf("upload finish:%s", hstr)))}func newRouter() http.Handler { hdl := mux.NewRouter() hdl.HandleFunc("/", upload) return hdl}func main() { http.ListenAndServe(":8877", newRouter())}
阅读全文
1 0
- [golang]简单文件上传服务
- Golang 实现简单的文件上传
- Golang实现简单的文件上传
- golang 上传文件
- golang web 文件上传
- Golang 上传文件
- golang 模拟文件上传
- golang 文件上传
- golang 上传文件
- golang 文件简单操作
- golang https服务简单介绍
- 简单的golang Web服务
- golang 上传文件到php
- 基于 lua-resty-upload 实现简单的文件上传服务
- Golang + HTML5 实现多文件上传
- Golang实现http文件上传小功能
- golang http获取上传文件 小记
- web 服务上传文件。
- WIN10下Message无法接收的问题
- 常用cmd
- 巨人科技笔试题解
- Android Studio主题设置、颜色背景配置
- 开源的推荐系统
- [golang]简单文件上传服务
- Python数据处理笔记——matplotlib篇(一)
- WOJ1419-无聊的游戏
- 八皇后问题(递归,回溯)
- Android 6.0 运行时权限处理完全解析
- 深度学习(七)—— GAN
- 51nod 1420 数袋鼠好有趣
- java中基础数据和引用类型对栈堆使用
- 图像识别 常用数据集