这篇文章主要讲解了“怎么使用Go实现文件分片上传”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Go实现文件分片上传”吧!
基本思想是,在web端将上传的文件进行分片处理,然后向服务端发送上传请求(UploadRequest)包括文件名,MD5,文件大小,和文件总片数。
然后开始一片一片的上传(Upload)。如果暂停了或者断网失败了,就记录当前上传片的片数,下一次上传,重发UploadRequest,chunkPos为续传位置片数,然后从这个记录的片数开始接着上传(Upload)。在服务端,记录首次请求的文件名,MD5,文件大小,文件总片数,然后开始接收每一片,将每一个片保存为一个单独的文件,在接收完最后一个片时,将所有的分片份文件进行合并。保存为对应的文件名。最后再校验MD5码。
对于不同用户的请求,将文件上传的信息存储到session中,当前文件上传暂停后,上传其它文件时,向session增加新文件的信息。暂停后,接着上传最开始的文件,则直接从session中取得该文件的信息,包括文件名,上传到某一个片的信息等。
服务端实现代码:
func UploadRequest(w http.ResponseWriter, r *http.Request) { log.WithFields(log.Fields{ "HTTP": r.Method, "FUNC": "UploadRequest", }).Info("HTTP REQUEST") header := w.Header() header.Add("Content-Type", "application/json") /* session authentication */ sess := session.GlobalSessions.SessionCheck(w, r) if sess == nil { log.Error("sess check error") fmt.Fprintf(w, config.FmtStr, "3000", "error", "session check error") return } defer r.Body.Close() con, _ := ioutil.ReadAll(r.Body) //获取post的body数据 log.Info("UploadRequest json: ", string(con)) var uploadReq uploadRequest err := json.Unmarshal([]byte(con), &uploadReq) /* 解析json字符串数据到结构体中 */ if err != nil { log.Error("json unmarshal error") fmt.Fprintf(w, config.FmtStr, "2000", "error", "json unmarshal error") return } switch uploadReq.Option { case "reUploadFile": { sess.Set("currentFile", uploadReq) err := os.Remove("./tmp/" + uploadReq.FileName + "/" + uploadReq.FileName + "_" + uploadReq.ChunkPos) if err != nil { log.Error(err) } fmt.Fprintf(w, config.FmtStr, "1000", "success", "reupload request success") } case "uploadFile": { err = os.Mkdir("./tmp/"+uploadReq.FileName, 0777) if err != nil { log.Info(err) fmt.Fprintf(w, config.FmtStr, "3000", "error", "mkdir error") } else { log.Infof("create dir %s ok\r\n", uploadReq.FileName) } // 文件上传信息保存,保存到session中,用于分片续传时使用 sess.Set(uploadReq.FileName, uploadReq) sess.Set("currentFile", uploadReq) fmt.Fprintf(w, config.FmtStr, "1000", "success", "upload request success") } case "uploadCancel": { err = os.RemoveAll("./tmp/" + uploadReq.FileName) if err != nil { log.Fatal(err) } sess.Delete(uploadReq.FileName) fmt.Fprintf(w, config.FmtStr, "1000", "success", "upload file success") return } default: { fmt.Fprintf(w, config.FmtStr, "2000", "error", "upload request option error") } } } /* 分片上传功能实现 */ func Upload(w http.ResponseWriter, r *http.Request) { log.WithFields(log.Fields{ "HTTP": r.Method, "FUNC": "Upload", }).Info("HTTP REQUEST") header := w.Header() header.Add("Content-Type", "application/json") /* session authentication */ sess := globalSessions.SessionCheck(w, r) //session 检查的接口封装 if sess == nil { log.Error("session check failed") fmt.Fprintf(w, config.FmtStr, "3000", "error", "session check failed") return } current_dir := sess.Get("current_dir") log.Info("current dir: ", current_dir) up_dir := sess.Get("up_dir") log.Info("up_dir: ", up_dir) // var current_dir string = "./river/test" /* 表单上传文件 */ if r.Method == "GET" { crutime := time.Now().Unix() h := md5.New() io.WriteString(h, strconv.FormatInt(crutime, 10)) token := fmt.Sprintf("%x", h.Sum(nil)) t, _ := template.ParseFiles("upload.gtpl") t.Execute(w, token) } else { r.ParseMultipartForm(32 << 20) file, _, err := r.FormFile("uploadfile") if err != nil { log.Error(err) return } defer file.Close() var uploadFileInfo uploadRequest uploadFileInfo = sess.Get("currentFile").(uploadRequest) // fmt.Fprintf(w, "%v", sess.Get("fileName")) var fileName string = uploadFileInfo.FileName + "_" + uploadFileInfo.ChunkPos /* 判断上传的文件是否已经存在,可能发生同名的情况 */ log.Debug("fileName: ", fileName) _, err = os.Stat("./tmp/" + uploadFileInfo.FileName + "/" + fileName) var f *os.File if err == nil { fmt.Fprintf(w, config.FmtStr, "3000", "error", "file is exsited") return } else { f, err = os.OpenFile("./tmp/"+uploadFileInfo.FileName+"/"+fileName, os.O_WRONLY|os.O_CREATE, 0666) // 此处假设当前目录下已存在test目录 } defer f.Close() if err != nil { log.Error(err) fmt.Fprintf(w, config.FmtStr, "3000", "error", "create file error") return } io.Copy(f, file) //这里进行大文件copy会导致内存占用过大。一段时候后会进行GC回收 log.Info("copy ok") // 判断是否单个文件是否上传完毕,上传完毕则合并文件到目标文件夹 if uploadFileInfo.ChunkPos == uploadFileInfo.ChunkNum { f.Close() _, err = os.Stat(current_dir.(string) + uploadFileInfo.FileName) var fii *os.File if err == nil { // 如果已经存在这个文件了,则在当前文件名后增加日期 time := time.Now().Format("2006-01-02_15-04-05") log.Debug("Time: ", time) var fileName []byte = []byte(current_dir.(string) + uploadFileInfo.FileName) var fileNameFinal []byte = fileName log.Debug("file_name: ", string(fileName)) var preFileName []byte var sufFileName []byte for i := len(fileName) - 1; i > 0; i-- { if fileName[i] == '.' { preFileName = fileName[:i] // 文件名前缀 test sufFileName = fileName[i:] // 文件名后缀 .txt fileNameFinal = []byte(string(preFileName) + "_" + time + string(sufFileName)) log.Info("fileNameFile: ", fileNameFinal) break } } fii, err = os.OpenFile(string(fileNameFinal), os.O_WRONLY|os.O_CREATE, 0777) // 此处假设当前目录下已存在test目录 } else { fii, err = os.OpenFile(current_dir.(string)+uploadFileInfo.FileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm) } defer fii.Close() if err != nil { log.Error(err) fmt.Fprintf(w, config.FmtStr, "3000", "error", "Open object file error") return } index, _ := strconv.Atoi(uploadFileInfo.ChunkNum) for i := 1; i <= index; i++ { f11, err := os.OpenFile("./tmp/"+uploadFileInfo.FileName+"/"+uploadFileInfo.FileName+"_"+strconv.Itoa(int(i)), os.O_RDONLY, os.ModePerm) if err != nil { log.Error(err) fmt.Fprintf(w, config.FmtStr, "3000", "error", "Open slice file error") return } b, err := ioutil.ReadAll(f11) if err != nil { log.Error(err) fmt.Fprintf(w, config.FmtStr, "3000", "error", "ioutil readall error") return } fii.Write(b) f11.Close() } err = os.RemoveAll("./tmp/" + uploadFileInfo.FileName) if err != nil { log.Error(err) } sess.Delete(uploadFileInfo.FileName) fii.Close() if uploadFileInfo.MD5 == FileMD5(fii.Name()) { err := os.Remove(current_dir.(string) + fii.Name()) if err != nil { log.Error(err) } fmt.Fprintf(w, config.FmtStr, "3000", "error", "upload file md5 error") } else { fmt.Fprintf(w, config.FmtStr, "1000", "success", "upload one file all slice success") } return } chunkPos, err := strconv.Atoi(uploadFileInfo.ChunkPos) if err != nil { log.Error(err) } uploadFileInfo.ChunkPos = strconv.Itoa(chunkPos + 1) sess.Set(uploadFileInfo.FileName, uploadFileInfo) sess.Set("currentFile", uploadFileInfo) fmt.Fprintf(w, config.FmtStr, "1000", "success", "upload file success") } } func FileMD5(file string) string { f, err := os.Open(file) defer f.Close() if err != nil { log.Info(err) return "" } buffer, _ := ioutil.ReadAll(f) data := buffer has := md5.Sum(data) md5str := fmt.Sprintf("%x", has) return md5str }
感谢各位的阅读,以上就是“怎么使用Go实现文件分片上传”的内容了,经过本文的学习后,相信大家对怎么使用Go实现文件分片上传这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。