package util
|
|
import (
|
"basic.com/pubsub/protomsg.git"
|
"bytes"
|
"encoding/json"
|
"errors"
|
"fmt"
|
"io"
|
"log"
|
"mime/multipart"
|
"net/http"
|
"time"
|
"gocv.io/x/gocv"
|
)
|
// 上传图片(二进制流)
|
func PostFormBufferData(uri string, img protomsg.Image, fileName string,) (maps map[string]interface{}, err0 error) {
|
// 要指定转byte的格式
|
imgs := gocv.NewMat()
|
imgs, _ = gocv.NewMatFromBytes(int(img.Height), int(img.Width), gocv.MatTypeCV8UC3, img.Data)
|
fdata,_ := gocv.IMEncode(".jpg",imgs)
|
body := &bytes.Buffer{}
|
writer := multipart.NewWriter(body)
|
_, err := writer.CreateFormFile("file", fileName)
|
if err != nil {
|
return nil, err
|
}
|
boundary := writer.Boundary()
|
//close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary)
|
close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary))
|
file := bytes.NewBuffer(fdata)
|
request_reader := io.MultiReader(body, file, close_buf)
|
//_, err = io.Copy(part, file)
|
//writer.WriteField(key, val)
|
request, err := http.NewRequest("POST", uri, request_reader)
|
request.Header.Add("Content-Type", writer.FormDataContentType())
|
timeout := time.Duration(5 * time.Second) //超时时间50ms
|
client := &http.Client{Timeout: timeout}
|
resp, err := client.Do(request)
|
if err != nil {
|
log.Fatal(err)
|
return nil, err
|
}
|
defer func() {
|
if r := recover(); r != nil {
|
fmt.Printf("panic的内容%v\n", r)
|
msg := "上传图片服务器异常"
|
if _, ok := r.(error); ok {
|
msg = r.(error).Error()
|
fmt.Println("panic--recover()得到的是error类型")
|
}
|
if _, ok := r.(string); ok {
|
msg = r.(string)
|
fmt.Println("panic--recover()得到的是string类型")
|
}
|
err0 = errors.New(msg)
|
}
|
}()
|
defer resp.Body.Close()
|
{
|
body := &bytes.Buffer{}
|
_, err := body.ReadFrom(resp.Body)
|
if err != nil {
|
log.Fatal(err)
|
}
|
fmt.Println(resp.StatusCode)
|
//fmt.Println(resp.Header)
|
fmt.Println(body)
|
//decoder := json.NewDecoder(strings.NewReader(body.String()))
|
decoder := make(map[string]interface{})
|
if err := json.Unmarshal([]byte(body.String()), &decoder); err != nil {
|
return nil, err
|
}
|
return decoder, nil
|
}
|
}
|
|
// 上传图片(二进制流)
|
func PostFormBufferData1(uri string, fdata []byte, fileName string,) (maps map[string]interface{}, err0 error) {
|
// 要指定转byte的格式
|
//imgs := gocv.NewMat()
|
//imgs, _ = gocv.NewMatFromBytes(1000, 1000, gocv.MatTypeCV8UC3, fbada)
|
//fdata,_ := gocv.IMEncode(".jpg",imgs)
|
body := &bytes.Buffer{}
|
writer := multipart.NewWriter(body)
|
_, err := writer.CreateFormFile("file", fileName)
|
if err != nil {
|
return nil, err
|
}
|
boundary := writer.Boundary()
|
//close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary)
|
close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary))
|
file := bytes.NewBuffer(fdata)
|
request_reader := io.MultiReader(body, file, close_buf)
|
//_, err = io.Copy(part, file)
|
//writer.WriteField(key, val)
|
request, err := http.NewRequest("POST", uri, request_reader)
|
request.Header.Add("Content-Type", writer.FormDataContentType())
|
timeout := time.Duration(5 * time.Second) //超时时间50ms
|
client := &http.Client{Timeout: timeout}
|
resp, err := client.Do(request)
|
if err != nil {
|
log.Fatal(err)
|
return nil, err
|
}
|
defer func() {
|
if r := recover(); r != nil {
|
fmt.Printf("panic的内容%v\n", r)
|
msg := "上传图片服务器异常"
|
if _, ok := r.(error); ok {
|
msg = r.(error).Error()
|
fmt.Println("panic--recover()得到的是error类型")
|
}
|
if _, ok := r.(string); ok {
|
msg = r.(string)
|
fmt.Println("panic--recover()得到的是string类型")
|
}
|
err0 = errors.New(msg)
|
}
|
}()
|
defer resp.Body.Close()
|
{
|
body := &bytes.Buffer{}
|
_, err := body.ReadFrom(resp.Body)
|
if err != nil {
|
log.Fatal(err)
|
}
|
fmt.Println(resp.StatusCode)
|
//fmt.Println(resp.Header)
|
fmt.Println(body)
|
//decoder := json.NewDecoder(strings.NewReader(body.String()))
|
decoder := make(map[string]interface{})
|
if err := json.Unmarshal([]byte(body.String()), &decoder); err != nil {
|
return nil, err
|
}
|
return decoder, nil
|
}
|
}
|