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");
}