欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang?gin框架實(shí)現(xiàn)大文件的流式上傳功能

 更新時(shí)間:2022年07月14日 11:02:14   作者:ahfuzhang  
這篇文章主要介紹了golang?gin框架中實(shí)現(xiàn)大文件的流式上傳,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一般來(lái)說(shuō),通過(guò)c.Request.FormFile()獲取文件的時(shí)候,所有內(nèi)容都全部讀到了內(nèi)存。如果是個(gè)巨大的文件,則可能內(nèi)存會(huì)爆掉;且,有的時(shí)候我們需要一邊上傳一邊處理。
以下的代碼實(shí)現(xiàn)了大文件流式上傳。
還非常不完美,但是可以作為參考:

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>upload file</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/gin_upload">
    <input type="file" name="ff" multiple="multiple"/><br/>
    <input type="submit" value="提交"/>
</form>
</body>

gin_stream_upload_file.go

/*
本例子實(shí)現(xiàn)了gin框架下的多個(gè)大文件流式上傳,避免了文件內(nèi)容存在內(nèi)存而無(wú)法支持大文件的情況
*/
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"os"
	"bytes"
	"io"
	"log"
	"strconv"
	"strings"
)

/// 解析多個(gè)文件上傳中,每個(gè)具體的文件的信息
type FileHeader struct{
	ContentDisposition string
	Name string
	FileName string			///< 文件名
	ContentType string
	ContentLength int64
}

/// 解析描述文件信息的頭部
/// @return FileHeader 文件名等信息的結(jié)構(gòu)體
/// @return bool 解析成功還是失敗
func ParseFileHeader(h []byte) (FileHeader, bool){
	arr := bytes.Split(h, []byte("\r\n"))
	var out_header FileHeader
	out_header.ContentLength = -1
	const (
		CONTENT_DISPOSITION = "Content-Disposition: "
		NAME = "name=\""
		FILENAME = "filename=\""
		CONTENT_TYPE = "Content-Type: "
		CONTENT_LENGTH = "Content-Length: "
	)
	for _,item := range arr{
		if bytes.HasPrefix(item, []byte(CONTENT_DISPOSITION)){
			l := len(CONTENT_DISPOSITION)
			arr1 := bytes.Split(item[l:], []byte("; "))
			out_header.ContentDisposition = string(arr1[0])
			if bytes.HasPrefix(arr1[1], []byte(NAME)){
				out_header.Name = string(arr1[1][len(NAME):len(arr1[1])-1])
			}
			l = len(arr1[2])
			if bytes.HasPrefix(arr1[2], []byte(FILENAME)) && arr1[2][l-1]==0x22{
				out_header.FileName = string(arr1[2][len(FILENAME):l-1])
			}
		} else if bytes.HasPrefix(item, []byte(CONTENT_TYPE)){
			l := len(CONTENT_TYPE)
			out_header.ContentType = string(item[l:])
		} else if bytes.HasPrefix(item, []byte(CONTENT_LENGTH)){
			l := len(CONTENT_LENGTH)
			s := string(item[l:])
			content_length,err := strconv.ParseInt(s, 10, 64)
			if err!=nil{
				log.Printf("content length error:%s", string(item))
				return out_header, false
			} else {
				out_header.ContentLength = content_length
			}
		} else {
			log.Printf("unknown:%s\n", string(item))
		}
	}
	if len(out_header.FileName)==0{
		return out_header,false
	}
	return out_header,true
}

/// 從流中一直讀到文件的末位
/// @return []byte 沒(méi)有寫(xiě)到文件且又屬于下一個(gè)文件的數(shù)據(jù)
/// @return bool 是否已經(jīng)讀到流的末位了
/// @return error 是否發(fā)生錯(cuò)誤
func ReadToBoundary(boundary []byte, stream io.ReadCloser, target io.WriteCloser)([]byte, bool, error){
	read_data := make([]byte, 1024*8)
	read_data_len := 0
	buf := make([]byte, 1024*4)
	b_len := len(boundary)
	reach_end := false
	for ;!reach_end; {
		read_len, err := stream.Read(buf)
		if err != nil {
			if err != io.EOF && read_len<=0 {
				return nil, true, err
			}
			reach_end = true
		}
		//todo: 下面這一句很蠢,值得優(yōu)化
		copy(read_data[read_data_len:], buf[:read_len])  //追加到另一塊buffer,僅僅只是為了搜索方便
		read_data_len += read_len
		if (read_data_len<b_len+4){
			continue
		}
		loc := bytes.Index(read_data[:read_data_len], boundary)
		if loc>=0{
			//找到了結(jié)束位置
			target.Write(read_data[:loc-4])
			return read_data[loc:read_data_len],reach_end, nil
		}

		target.Write(read_data[:read_data_len-b_len-4])
		copy(read_data[0:], read_data[read_data_len-b_len-4:])
		read_data_len = b_len + 4
	}
	target.Write(read_data[:read_data_len])
	return nil, reach_end, nil
}

/// 解析表單的頭部
/// @param read_data 已經(jīng)從流中讀到的數(shù)據(jù)
/// @param read_total 已經(jīng)從流中讀到的數(shù)據(jù)長(zhǎng)度
/// @param boundary 表單的分割字符串
/// @param stream 輸入流
/// @return FileHeader 文件名等信息頭
///			[]byte 已經(jīng)從流中讀到的部分
///			error 是否發(fā)生錯(cuò)誤
func ParseFromHead(read_data []byte, read_total int, boundary []byte, stream io.ReadCloser)(FileHeader, []byte, error){
	buf := make([]byte, 1024*4)
	found_boundary := false
	boundary_loc := -1
	var file_header FileHeader
	for {
		read_len, err := stream.Read(buf)
		if err!=nil{
			if err!=io.EOF{
				return file_header, nil, err
			}
			break
		}
		if read_total+read_len>cap(read_data){
			return file_header, nil, fmt.Errorf("not found boundary")
		}
		copy(read_data[read_total:], buf[:read_len])
		read_total += read_len
		if !found_boundary {
			boundary_loc = bytes.Index(read_data[:read_total], boundary)
			if -1 == boundary_loc {
				continue
			}
			found_boundary = true
		}
		start_loc := boundary_loc+len(boundary)
		file_head_loc := bytes.Index(read_data[start_loc:read_total], []byte("\r\n\r\n"))
		if -1==file_head_loc{
			continue
		}
		file_head_loc += start_loc
		ret := false
		file_header,ret = ParseFileHeader(read_data[start_loc:file_head_loc])
		if !ret{
			return file_header,nil,fmt.Errorf("ParseFileHeader fail:%s", string(read_data[start_loc:file_head_loc]))
		}
		return file_header, read_data[file_head_loc+4:read_total], nil
	}
	return file_header,nil,fmt.Errorf("reach to sream EOF")
}

func main(){
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	r := gin.Default()
	r.StaticFile("/upload.html", "./upload.html")


	r.POST("/gin_upload", func(c *gin.Context) {
		var content_length int64
		content_length = c.Request.ContentLength
		if content_length<=0 || content_length>1024*1024*1024*2{
			log.Printf("content_length error\n")
			return
		}
		content_type_,has_key := c.Request.Header["Content-Type"]
		if  !has_key{
			log.Printf("Content-Type error\n")
			return
		}
		if len(content_type_)!=1{
			log.Printf("Content-Type count error\n")
			return
		}
		content_type := content_type_[0]
		const BOUNDARY string = "; boundary="
		loc := strings.Index(content_type, BOUNDARY)
		if -1==loc{
			log.Printf("Content-Type error, no boundary\n")
			return
		}
		boundary := []byte(content_type[(loc+len(BOUNDARY)):])
		log.Printf("[%s]\n\n", boundary)
		//
		read_data := make([]byte, 1024*12)
		var read_total int = 0
		for {
			file_header, file_data, err := ParseFromHead(read_data, read_total, append(boundary, []byte("\r\n")...), c.Request.Body)
			if err != nil {
				log.Printf("%v", err)
				return
			}
			log.Printf("file :%s\n", file_header.FileName)
			//
			f, err := os.Create(file_header.FileName)
			if err != nil {
				log.Printf("create file fail:%v\n", err)
				return
			}
			f.Write(file_data)
			file_data = nil

			//需要反復(fù)搜索boundary
			temp_data, reach_end, err := ReadToBoundary(boundary, c.Request.Body, f)
			f.Close()
			if err != nil {
				log.Printf("%v\n", err)
				return
			}
			if reach_end{
				break
			} else {
				copy(read_data[0:], temp_data)
				read_total = len(temp_data)
				continue
			}
		}
		//
		c.JSON(200, gin.H{
			"message": fmt.Sprintf("%s", "ok"),
		})
	})
	r.Run()
}

到此這篇關(guān)于golang gin框架中實(shí)現(xiàn)大文件的流式上傳的文章就介紹到這了,更多相關(guān)golang 大文件流式上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go通過(guò)不變性?xún)?yōu)化程序詳解

    Go通過(guò)不變性?xún)?yōu)化程序詳解

    這篇文章主要為大家介紹了Go通過(guò)不變性?xún)?yōu)化程序?qū)嵗斀?,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Go語(yǔ)言并發(fā)之原子操作詳解

    Go語(yǔ)言并發(fā)之原子操作詳解

    代碼中的加鎖操作因?yàn)樯婕皟?nèi)核態(tài)的上下文切換會(huì)比較耗時(shí)、代價(jià)比較高。針對(duì)基本數(shù)據(jù)類(lèi)型我們還可以使用原子操作來(lái)保證并發(fā)安全,本文就來(lái)和大家詳細(xì)聊聊,需要的可以參考下
    2022-12-12
  • golang gorm 計(jì)算字段和獲取sum()值的實(shí)現(xiàn)

    golang gorm 計(jì)算字段和獲取sum()值的實(shí)現(xiàn)

    這篇文章主要介紹了golang gorm 計(jì)算字段和獲取sum()值的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Go語(yǔ)言的os包中常用函數(shù)初步歸納

    Go語(yǔ)言的os包中常用函數(shù)初步歸納

    這篇文章主要介紹了Go語(yǔ)言的os包中常用函數(shù)初步歸納,用于一些和系統(tǒng)交互功能的實(shí)現(xiàn),需要的朋友可以參考下
    2015-10-10
  • Golang實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)Stack(堆棧)的示例詳解

    Golang實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)Stack(堆棧)的示例詳解

    在計(jì)算機(jī)科學(xué)中,stack(棧)是一種基本的數(shù)據(jù)結(jié)構(gòu),它是一種線性結(jié)構(gòu),具有后進(jìn)先出(Last In First Out)的特點(diǎn)。本文將通過(guò)Golang實(shí)現(xiàn)堆棧,需要的可以參考一下
    2023-04-04
  • Golang 內(nèi)存管理簡(jiǎn)單技巧詳解

    Golang 內(nèi)存管理簡(jiǎn)單技巧詳解

    這篇文章主要為大家介紹了Golang 內(nèi)存管理簡(jiǎn)單技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 使用go的interface案例實(shí)現(xiàn)多態(tài)范式操作

    使用go的interface案例實(shí)現(xiàn)多態(tài)范式操作

    這篇文章主要介紹了使用go的interface案例實(shí)現(xiàn)多態(tài)范式操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • GO語(yǔ)言導(dǎo)入自己寫(xiě)的包(同級(jí)目錄和不同目錄)

    GO語(yǔ)言導(dǎo)入自己寫(xiě)的包(同級(jí)目錄和不同目錄)

    本文介紹了如何在Go語(yǔ)言項(xiàng)目中導(dǎo)入同級(jí)目錄和不同目錄的包,詳細(xì)解釋了創(chuàng)建文件結(jié)構(gòu)、編寫(xiě)主函數(shù)、同級(jí)目錄和不同目錄方法的調(diào)用,適合初學(xué)者參考,幫助理解Go項(xiàng)目的基本構(gòu)建和包管理
    2024-09-09
  • Go高級(jí)特性探究之recover捕獲panic詳解

    Go高級(jí)特性探究之recover捕獲panic詳解

    在Go語(yǔ)言中,當(dāng)程序出現(xiàn)panic(即運(yùn)行時(shí)錯(cuò)誤)時(shí),程序會(huì)立即停止當(dāng)前的執(zhí)行流程,而recover函數(shù)的作用就是捕獲這個(gè)panic,下面就來(lái)看看具體是怎么操作的吧
    2023-06-06
  • Golang二進(jìn)制反匯編問(wèn)題

    Golang二進(jìn)制反匯編問(wèn)題

    這篇文章主要介紹了Golang二進(jìn)制反匯編問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評(píng)論