@ -0,0 +1,127 @@ |
|||
package fileuploaddownload |
|||
|
|||
import ( |
|||
"bytes" |
|||
"encoding/json" |
|||
"fmt" |
|||
"io" |
|||
"io/ioutil" |
|||
"mime/multipart" |
|||
"net/http" |
|||
"os" |
|||
"strconv" |
|||
"time" |
|||
|
|||
"github.com/flipped-aurora/gin-vue-admin/server/commonus" |
|||
"github.com/flipped-aurora/gin-vue-admin/server/global" |
|||
) |
|||
|
|||
//上传文件返回前端
|
|||
type UpLoadFileStruct struct { |
|||
ID int64 `json:"ID"` |
|||
Key string `json:"key"` |
|||
Name string `json:"name"` |
|||
Tag string `json:"tag"` |
|||
Url string `json:"url"` |
|||
PhysicsPath string `json:"physicspath"` |
|||
CreatedAt time.Time `json:"CreatedAt"` |
|||
UpdatedAt time.Time `json:"UpdatedAt"` |
|||
FileSize int64 `json:"fileSize"` //大小
|
|||
} |
|||
|
|||
//文件基本属性
|
|||
type FileAttribute struct { |
|||
FileName string `json:"fileName"` |
|||
FileSize int64 `json:"fileSize"` |
|||
FileExt string `json:"fileExt"` |
|||
} |
|||
|
|||
//远程数据返回
|
|||
type CallBackFileData struct { |
|||
commonus.ReturnJson |
|||
Data RemoteUploadDataStruct `json:data` |
|||
} |
|||
|
|||
//远程上传返回
|
|||
type RemoteUploadDataStruct struct { |
|||
Callbackurl string `json:"callbackurl"` //预览地址
|
|||
FilePath string `json:"filePath"` //物理地址
|
|||
Ext string `json:"ext"` //文件后缀
|
|||
Names string `json:"names"` //文件新名称
|
|||
NewName string `json:"newName"` //文件原名称
|
|||
GetSize int64 `json:"getSize"` //文件大小
|
|||
GetOriginalMime string `json:"getOriginalMime"` //上传方式
|
|||
Physicspath string `json:"physicspath"` //相对路径
|
|||
|
|||
} |
|||
|
|||
//上传到远程服务器
|
|||
func postFormDataWithSingleFile(filePath string, fileType int) (CallBackFileData CallBackFileData) { |
|||
client := http.Client{} |
|||
bodyBuf := &bytes.Buffer{} |
|||
bodyWrite := multipart.NewWriter(bodyBuf) |
|||
file, err := os.Open(filePath) |
|||
defer file.Close() |
|||
if err != nil { |
|||
CallBackFileData.Code = 100 |
|||
CallBackFileData.Msg = "打开文件失败!" |
|||
return |
|||
} |
|||
|
|||
var upFileMd5 commonus.Md5Encryption |
|||
jsonSend := commonus.MapOut() |
|||
jsonSend["type"] = fileType |
|||
mapJson, _ := json.Marshal(jsonSend) |
|||
upFileMd5.Code = string(mapJson) |
|||
upSign := upFileMd5.Md5EncryptionAlgorithm() |
|||
// fmt.Println(upSign)
|
|||
// return
|
|||
bodyWrite.WriteField("type", strconv.Itoa(fileType)) |
|||
bodyWrite.WriteField("sign", upSign) |
|||
// file 为key
|
|||
fileWrite, err := bodyWrite.CreateFormFile("file", filePath) |
|||
_, err = io.Copy(fileWrite, file) |
|||
if err != nil { |
|||
CallBackFileData.Code = 101 |
|||
CallBackFileData.Msg = "创建文件发送包失败!" |
|||
return |
|||
} |
|||
bodyWrite.Close() //要关闭,会将w.w.boundary刷写到w.writer中
|
|||
// 创建请求
|
|||
contentType := bodyWrite.FormDataContentType() |
|||
req, err := http.NewRequest(http.MethodPost, global.GVA_CONFIG.MyConfig.Visit, bodyBuf) |
|||
if err != nil { |
|||
CallBackFileData.Code = 102 |
|||
CallBackFileData.Msg = "远程上传失败!" |
|||
return |
|||
} |
|||
// 设置头
|
|||
req.Header.Set("Content-Type", contentType) |
|||
resp, err := client.Do(req) |
|||
if err != nil { |
|||
CallBackFileData.Code = 103 |
|||
CallBackFileData.Msg = "读取返回文件失败" |
|||
return |
|||
} |
|||
defer resp.Body.Close() |
|||
b, err := ioutil.ReadAll(resp.Body) |
|||
if err != nil { |
|||
CallBackFileData.Code = 104 |
|||
CallBackFileData.Msg = "解析返回文件失败" |
|||
return |
|||
} |
|||
// var CallBackFileData CallBackFileData
|
|||
jsonErr := json.Unmarshal(b, &CallBackFileData) |
|||
fmt.Println(string(b)) |
|||
// fmt.Println(CallBackFileData)
|
|||
if jsonErr != nil { |
|||
CallBackFileData.Code = 105 |
|||
// CallBackFileData.Msg = "解析返回数据失败!"
|
|||
return |
|||
} |
|||
if CallBackFileData.Code != 1 { |
|||
return |
|||
} |
|||
// fmt.Println(string(b))
|
|||
return |
|||
} |
|||
@ -0,0 +1,157 @@ |
|||
package fileuploaddownload |
|||
|
|||
import ( |
|||
"bytes" |
|||
"fmt" |
|||
"io" |
|||
"io/ioutil" |
|||
"mime/multipart" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
"strings" |
|||
) |
|||
|
|||
func httpGet(urlPath string) { |
|||
resp, err := http.Get(urlPath) |
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
|
|||
defer resp.Body.Close() |
|||
body, err := ioutil.ReadAll(resp.Body) |
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
fmt.Printf("Get---------->%v\n", string(body)) |
|||
// fmt.Println(string(body))
|
|||
} |
|||
|
|||
func httpPost(urlPath string) { |
|||
resp, err := http.Post(urlPath, |
|||
"application/x-www-form-urlencoded", |
|||
strings.NewReader("key=1&sign=250&file=G:\\goobject\\src\\git_public\\gin-vue-admin\\gin_server_admin\\uploads\\file310dcbbf4cce62f762a2aaa148d556bd_20211222135245.jpg")) |
|||
if err != nil { |
|||
fmt.Println(err) |
|||
} |
|||
|
|||
defer resp.Body.Close() |
|||
body, err := ioutil.ReadAll(resp.Body) |
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
|
|||
// fmt.Println(string(body))
|
|||
fmt.Printf("Post---------->%v\n", string(body)) |
|||
} |
|||
|
|||
func httpPostForm(urlPath string) { |
|||
resp, err := http.PostForm(urlPath, |
|||
url.Values{"key": {"1"}, "sign": {"123"}, "file": {"G:\\goobject\\src\\git_public\\gin-vue-admin\\gin_server_admin\\uploads\\file310dcbbf4cce62f762a2aaa148d556bd_20211222135245.jpg"}}) |
|||
|
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
|
|||
defer resp.Body.Close() |
|||
body, err := ioutil.ReadAll(resp.Body) |
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
|
|||
// fmt.Println(string(body))
|
|||
fmt.Printf("PostForm---------->%v\n", string(body)) |
|||
} |
|||
|
|||
func httpDoes(urlPath string) { |
|||
client := &http.Client{} |
|||
|
|||
req, err := http.NewRequest("POST", urlPath, strings.NewReader("key=1&sign=250&file=G:\\goobject\\src\\git_public\\gin-vue-admin\\gin_server_admin\\uploads\\file310dcbbf4cce62f762a2aaa148d556bd_20211222135245.jpg")) |
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
|
|||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
|||
req.Header.Set("Cookie", "name=baidu") |
|||
|
|||
resp, err := client.Do(req) |
|||
|
|||
defer resp.Body.Close() |
|||
|
|||
body, err := ioutil.ReadAll(resp.Body) |
|||
if err != nil { |
|||
// handle error
|
|||
} |
|||
|
|||
// fmt.Println(string(body))
|
|||
fmt.Printf("NewRequest-----POST----->%v\n", string(body)) |
|||
} |
|||
|
|||
func postFile(filename string, targetUrl string) error { |
|||
bodyBuf := &bytes.Buffer{} |
|||
bodyWriter := multipart.NewWriter(bodyBuf) |
|||
|
|||
//关键的一步操作
|
|||
fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filename) |
|||
if err != nil { |
|||
fmt.Println("error writing to buffer") |
|||
return err |
|||
} |
|||
|
|||
//打开文件句柄操作
|
|||
fh, err := os.Open(filename) |
|||
if err != nil { |
|||
fmt.Println("error opening file") |
|||
return err |
|||
} |
|||
defer fh.Close() |
|||
|
|||
//iocopy
|
|||
_, err = io.Copy(fileWriter, fh) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
|
|||
contentType := bodyWriter.FormDataContentType() |
|||
bodyWriter.Close() |
|||
|
|||
resp, err := http.Post(targetUrl, contentType, bodyBuf) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
defer resp.Body.Close() |
|||
resp_body, err := ioutil.ReadAll(resp.Body) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
// fmt.Println(resp.Status)
|
|||
// fmt.Println(string(resp_body))
|
|||
|
|||
fmt.Printf("NewRequest---%v--POST----->%v\n", resp.Status, string(resp_body)) |
|||
return nil |
|||
} |
|||
|
|||
//创建表单文件
|
|||
func addFile(urlPath string) { |
|||
var buff bytes.Buffer |
|||
// 创建一个Writer
|
|||
writer := multipart.NewWriter(&buff) |
|||
writer.WriteField("key", "1") |
|||
writer.WriteField("sign", "250") |
|||
// 写入图片字段
|
|||
// CreateFormFile第一个参数是 表单对应的字段名
|
|||
// 第二个字段是对应上传文件的名称
|
|||
w, err := writer.CreateFormFile("file", "14250.jpg") |
|||
if err != nil { |
|||
fmt.Println("创建文件失败: ", err) |
|||
return |
|||
} |
|||
data, err := ioutil.ReadFile("G:\\goobject\\src\\git_public\\gin-vue-admin\\gin_server_admin\\uploads\\file310dcbbf4cce62f762a2aaa148d556bd_20211222135245.jpg") |
|||
if err != nil { |
|||
fmt.Println("读取图片发生错误: ", err) |
|||
return |
|||
} |
|||
// 把文件内容写入
|
|||
w.Write(data) |
|||
writer.Close() |
|||
} |
|||
@ -0,0 +1,6 @@ |
|||
package config |
|||
|
|||
type MyConfigStruct struct { |
|||
Visit string `json:"visit"` |
|||
AppKey string `json:"appKey"` |
|||
} |
|||
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 146 KiB |
|
After Width: | Height: | Size: 157 KiB |
|
After Width: | Height: | Size: 204 KiB |
|
After Width: | Height: | Size: 172 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 43 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 64 KiB |