title: 使用 Go 自帶 net 庫和 GoFrame 分別實現 form 表單文件上傳與自定義 Cookie
date: 2021-09-17 13:26:00
toc: true
category:
- Golang
tags: - Golang
- net
- http
- 庫
- GoFrame
- 實現
- 表單
- 文件
- 上傳
- 自定義
- Cookie
本文實現使用 Go 語言調用鏈滴圖床。
下圖展示的內容為兩種方式分別上傳同一張圖片到鏈滴圖床。
代碼如下:
package main
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/glog"
)
const (
// loginUrl 登錄地址
loginUrl = "https://ld246.com/login"
// uploadImgUrl 圖片上傳地址
uploadImgUrl = "https://ld246.com/upload/editor"
// uploadImgUrl = "http://localhost:9999"
// uploadFilePath 上傳文件所在路徑
uploadFilePath = "C:/Users/biuaxia/Downloads/Snipaste_2021-09-07_15-03-20.png"
)
type loginReqData struct {
NameOrEmail string `json:"nameOrEmail"`
UserPassword string `json:"userPassword"`
Captcha string `json:"captcha"`
}
type loginRespData struct {
Msg string `json:"msg"`
Code int `json:"code"`
Goto string `json:"goto"`
TokenName string `json:"tokenName"`
Token string `json:"token"`
}
// main [鏈滴](http://ld246.com)圖床上傳接口
func main() {
data := getLoginRespData()
uploadImg(data)
uploadPic(data)
}
// uploadImg 使用 goFrame 實現文件上傳
func uploadImg(data loginRespData) {
m := make(map[string]string)
m[data.TokenName] = data.Token
r, e := g.Client().
Cookie(m).
Post(uploadImgUrl, "file[]=@file:"+uploadFilePath)
if e != nil {
glog.Error(e)
} else {
fmt.Println("uploadImg-resp:", string(r.ReadAll()))
err := r.Close()
if err != nil {
panic(err)
}
}
}
// uploadPic 使用原生實現文件上傳
// key:file 裡面放一個文件
// multipart/form-data 傳一個文件
func uploadPic(data loginRespData) {
m := make(map[string]string)
m[data.TokenName] = data.Token
var (
buffer = bytes.NewBuffer(nil)
writer = multipart.NewWriter(buffer)
)
formFile, _ := writer.CreateFormFile("file[]", filepath.Base(uploadFilePath))
file, _ := os.Open(uploadFilePath)
_, _ = io.Copy(formFile, file)
defer func(file *os.File) {
err := file.Close()
if err != nil {
panic(err)
}
}(file)
defer func(writer *multipart.Writer) {
err := writer.Close()
if err != nil {
panic(err)
}
}(writer)
req, _ := http.NewRequest("POST", uploadImgUrl, buffer)
req.Header.Set("Content-Type", writer.FormDataContentType())
req = req.WithContext(context.Background())
req.Header.Set("Cookie", data.TokenName+"="+data.Token)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47")
resp, _ := http.DefaultClient.Do(req)
readAll, _ := ioutil.ReadAll(resp.Body)
fmt.Println("uploadPic-resp:", string(readAll))
}
// getLoginRespData 獲取登錄信息,主要是響應得到的 Token,用來拼接圖床請求的 Cookie 信息
func getLoginRespData() loginRespData {
ld := loginReqData{
NameOrEmail: "你的賬號",
UserPassword: getPassword("你的賬號"),
Captcha: "",
}
// 準備請求內容
reqBody, _ := json.Marshal(ld)
log.Println("請求內容:", string(reqBody))
resp, _ := http.Post(loginUrl, "application/json", strings.NewReader(string(reqBody)))
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(resp.Body)
body, _ := ioutil.ReadAll(resp.Body)
log.Println("響應內容:", string(body))
var lrd loginRespData
_ = json.Unmarshal(body, &lrd)
return lrd
}
// getPassword 獲取 Md5 加密後的內容
func getPassword(password string) string {
h := md5.New()
h.Write([]byte(password))
return hex.EncodeToString(h.Sum(nil))
}