04-Gin文件上传和cookie

该文展示了如何在Go语言使用Gin框架实现文件上传功能,包括基础上传和带有时间戳的文件命名,以及Cookie的设置、获取和删除操作。通过定义FileService和CookieService处理业务逻辑,然后创建FileController和CookieController来绑定路由并提供HTTP接口。
摘要由CSDN通过智能技术生成

1,文件上传

1.1,新建 FileService

package service

import (
	"github.com/gin-gonic/gin"
	"go_gin/src/entity/base"
	"log"
	"os"
	"path"
	"strconv"
	"time"
)

type FileService struct {

}

var allowExtMap map[string]bool
func init() {
	allowExtMap = map[string]bool{
		".jpg" : true,
		".png" : true,
		".gif" : true,
		".jpeg" : true,
	}
}

/**
	上传图片
 */
func (fileService FileService)Upload(c *gin.Context)  {
	file,err := c.FormFile("file")
	result := base.BaseEntity{}
	if(err == nil){
		fileName := file.Filename
		filePath := "E:\\"+fileName
		c.SaveUploadedFile(file,filePath)
		log.Println(file,fileName)
		result.Success = true
		result.Value = filePath
		c.JSON(200,result)
	}else{
		result.Success = false
		result.Value = nil
		c.JSON(200,result)
	}
}

/**
	上传图片,带时间搓
*/
func (fileService FileService)SaveUploadByTime(c *gin.Context)  {
	file,err := c.FormFile("file")
	result := base.BaseEntity{}

	if(err == nil){
		//判断后缀
		extName := path.Ext(file.Filename)

		//判断文件是否合法
		_,ok := allowExtMap[extName]
		if(!ok){
			result.Success = false
			result.Value = ".上传文件类型不合法"
			c.JSON(200,result)
		}

		//创建文件保存目录
		day := time.Now().Format("2006/01/02")
		filePath := "./uploadImage/" + day

		//创建目录
		err := os.MkdirAll(filePath,0666)
		if(err != nil){
			result.Success = false
			result.Value = "创建文件失败"
			c.JSON(200,result)
		}

		//按照时间戳保存文件名
		unix := time.Now().UnixNano()
		fileName := strconv.FormatInt(unix,10) + extName;

		//上传文件
		filePath = filePath + "/" + fileName
		c.SaveUploadedFile(file,filePath)
		log.Println(file,fileName)

		result.Success = true
		result.Value = filePath
		c.JSON(200,result)
	}else{
		result.Success = false
		result.Value = nil
		c.JSON(200,result)
	}
}


1.2,新建 FileController

package controller

import (
	"github.com/gin-gonic/gin"
	"go_gin/src/demo07_fileUpload/service"
)

func FileController(engine *gin.Engine)  {
	fileRouters := engine.Group("/file")
	{
		//上传图片
		fileRouters.POST("/upload",service.FileService{}.Upload)

		//上传图片,带时间搓
		fileRouters.POST("/SaveUploadByTime",service.FileService{}.SaveUploadByTime)
	}
}

1.3,新建 demo01

package main

import (
	"github.com/gin-gonic/gin"
	"go_gin/src/demo07_fileUpload/controller"
)

func main() {
	router := gin.Default();
	//设置文件大小
	router.MaxMultipartMemory = 8 << 20
	controller.FileController(router)
	router.Run(":8080");
}

1.4 测试

在这里插入图片描述
在这里插入图片描述

2,cookie

2.1,CookieService

package service

import "github.com/gin-gonic/gin"

type CookieService struct {

}

func (cookieService CookieService)SetCookie(c *gin.Context)  {
	c.SetCookie("userName","李乃龙",3600,"/","localhost",false,true)
	c.String(200,"success")
}

func (cookieService CookieService)GetCookie(c *gin.Context)  {
	userName,_ := c.Cookie("userName")
	c.String(200,userName)
}

func (cookieService CookieService)DeleteCookie(c *gin.Context)  {
	c.SetCookie("userName","李乃龙",-1,"/","localhost",false,true)
	c.String(200,"success")
}

2.2,CookieController

package controller

import (
	"github.com/gin-gonic/gin"
	"go_gin/src/demo08_cookie/cookie/service"
)

func CookieController(engine *gin.Engine)  {
	cookieRouters := engine.Group("/cookie")
	{
		cookieRouters.POST("/set",service.CookieService{}.SetCookie)
		cookieRouters.POST("/get",service.CookieService{}.GetCookie)
		cookieRouters.POST("/delete",service.CookieService{}.DeleteCookie)
	}
}

2.3,demo01

package main

import (
	"github.com/gin-gonic/gin"
	"go_gin/src/demo08_cookie/cookie/controller"
)

func main() {
	router := gin.Default();
	controller.CookieController(router)
	router.Run(":8080");
}

2.4,测试

2.4.1,设置cookie

在这里插入图片描述

2.4.2,获取cookie

在这里插入图片描述

2.4.3 删除cookie

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值