dddd
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.8 KiB

4 years ago
package fileuploaddownload
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"unsafe"
"github.com/flipped-aurora/gin-vue-admin/server/commonus"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/gin-gonic/gin"
)
type FileUploadDownload struct{}
type urlData struct {
Type int `json:"typeese"`
Sign string `json:"sign"`
}
//上传文件(远程)
func (f *FileUploadDownload) LongRangeFileUpload(c *gin.Context) {
// var file example.ExaFileUploadAndDownload
// noSave := c.DefaultQuery("noSave", "0")
var reportStatistics urlData
err := c.ShouldBindJSON(&reportStatistics)
file, errs := c.FormFile("file")
// filePart, header, err := c.Request.FormFile("file")
outPut := commonus.MapOut()
// outPut["filePart"] = filePart
// outPut["header"] = header
// outPut["err"] = err
// outPut["noSave"] = noSave
outPut["file"] = file
outPut["errs"] = errs
outPut["err"] = err
outPut["reportStatistics"] = reportStatistics
// url := "http://docu.hxgk.net/uploadfileing/uploadimging"
var jsonPostSample JsonPostSample
jsonPostSample.SamplePost()
// client := &http.Client{}
// config := map[string]interface{}{}
// config["type"] = 0
// config["sign"] = "input"
// // config["afa"] = 2
// configdata, _ := json.Marshal(config)
// outPut["configdata"] = string(configdata)
// //json序列化
// post := "{\"type\":\"" + string(configdata) +
// "\",\"sign\":\"" + string(configdata) +
// "\"}"
// fmt.Println(url, "post", post)
// var jsonStr = []byte(post)
// fmt.Println("jsonStr", jsonStr)
// fmt.Println("new_str", bytes.NewBuffer(jsonStr))
// // req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
// posts := "type=1&sign=123"
// req, err := http.NewRequest("POST", url, strings.NewReader(posts))
// if err != nil {
// // handle error
// }
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// // req.Header.Set("Content-Type", "application/json")
// // req.Header.Set("Cookie", "name=anny")
// resp, err := client.Do(req)
// defer resp.Body.Close()
// body, err := ioutil.ReadAll(resp.Body)
// outPut["body"] = string(body)
response.Result(0, outPut, "获取成功", c)
}
func sendPost1(urlStr string) {
data := make(url.Values)
data["name"] = []string{"rnben"}
res, err := http.PostForm(urlStr, data)
if err != nil {
fmt.Println(err.Error())
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Printf("==============>%v\n", string(body))
}
func postData(urlStr string) {
http.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var (
name string = r.PostFormValue("type")
)
fmt.Printf("key is : %s\n", name)
}
})
err := http.ListenAndServe(":80", nil)
if err != nil {
fmt.Println(err.Error())
return
}
}
func httpDo(urlStr string) {
client := &http.Client{}
req, err := http.NewRequest("POST", urlStr, strings.NewReader("sign=cjb"))
if err != nil {
// handle error
}
// req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", "name=anny")
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Printf("Body------------------->%v\n", string(body))
}
//上传测试副本
func (f *FileUploadDownload) LongRangeFileUploadES(c *gin.Context) {
// var file example.ExaFileUploadAndDownload
// noSave := c.DefaultQuery("noSave", "0")
var reportStatistics urlData
err := c.ShouldBindJSON(&reportStatistics)
file, errs := c.FormFile("file")
// filePart, header, err := c.Request.FormFile("file")
outPut := commonus.MapOut()
// outPut["filePart"] = filePart
// outPut["header"] = header
// outPut["err"] = err
// outPut["noSave"] = noSave
outPut["file"] = file
outPut["errs"] = errs
// outPut["reportStatistics"] = reportStatistics
// if err != nil {
// // global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
// // response.FailWithMessage("接收文件失败", c)
// outPut["msgerr"] = "接收文件失败"
// // return
// } else {
// outPut["msgerr"] = "接收文件成功"
// }
// config := map[string]interface{}{}
// config["Id"] = 0
// config["afa"] = "input"
// config["afa"] = 2
// configdata, _ := json.Marshal(config)
// fmt.Println(config)
// body := bytes.NewBuffer([]byte(configdata))
// configdata, _ := json.Marshal(outPut)
// body := bytes.NewBuffer([]byte(configdata))
// resp, errkjh := http.Post("http://docs.hxgk.group/uploadfileing/uploadimging", "multipart/form-data;charset=utf-8", body)
// outPut["resp"] = resp
// outPut["errkjh"] = errkjh
DataUrlVal := make(map[string]string)
// DataUrlVal.Add("type", strconv.Itoa(reportStatistics.Type))
// DataUrlVal.Add("sign", reportStatistics.Sign)
DataUrlVal["type"] = strconv.Itoa(reportStatistics.Type)
DataUrlVal["sign"] = reportStatistics.Sign
// DataUrlValJson, _ := json.Marshal(DataUrlVal)
// DataUrlVal := url.Values{}
// url := "http://docs.hxgk.group/uploadfileing/uploadimging"
url := "http://docu.hxgk.net/uploadfileing/uploadimging"
// request, err := http.NewRequest("POST", url, strings.NewReader(string(DataUrlValJson)))
// if err != nil {
// response.Result(0, err, "打开链接失败", c)
// return
// }
// request.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
// // request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
// request.Header.Add("Accept-Encoding", "gzip, deflate, br")
// request.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4")
// request.Header.Add("Connection", "keep-alive")
// request.Header.Add("Content-Length", "25")
// request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// request.Header.Add("Cookie", "user_trace_token=20170425200852-dfbddc2c21fd492caac33936c08aef7e; LGUID=20170425200852-f2e56fe3-29af-11e7-b359-5254005c3644; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; hasDeliver=22; index_location_city=%E5%85%A8%E5%9B%BD; JSESSIONID=CEB4F9FAD55FDA93B8B43DC64F6D3DB8; TG-TRACK-CODE=search_code; SEARCH_ID=b642e683bb424e7f8622b0c6a17ffeeb; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1493122129,1493380366; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1493383810; _ga=GA1.2.1167865619.1493122129; LGSID=20170428195247-32c086bf-2c09-11e7-871f-525400f775ce; LGRID=20170428205011-376bf3ce-2c11-11e7-8724-525400f775ce; _putrc=AFBE3C2EAEBB8730")
// request.Header.Add("Host", "www.lagou.com")
// request.Header.Add("Origin", "https://www.lagou.com")
// request.Header.Add("Referer", "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=")
// request.Header.Add("X-Anit-Forge-Code", "0")
// request.Header.Add("X-Anit-Forge-Token", "None")
// request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
// request.Header.Add("X-Requested-With", "XMLHttpRequest")
// client := &http.Client{}
// responseww, err := client.Do(request)
// if err != nil {
// response.Result(102, err, "打开链接失败", c)
// return
// }
// defer responseww.Body.Close()
// body, err := ioutil.ReadAll(responseww.Body)
// outPut["body"] = string(body)
// outPut["DataUrlValJson"] = string(DataUrlValJson)
// PostWithFormData("POST", url, &DataUrlVal)
responsesss, err := http.Post(
url,
"application/x-www-form-urlencoded",
strings.NewReader("type=1&age=99"),
)
outPut["err"] = err
defer responsesss.Body.Close()
body, err := ioutil.ReadAll(responsesss.Body)
outPut["body"] = string(body)
httpDo(url)
response.Result(0, outPut, "获取成功", c)
}
type JsonPostSample struct {
}
func (this *JsonPostSample) SamplePost() {
song := make(map[string]interface{})
song["name"] = "李白"
song["timelength"] = 128
song["author"] = "李荣浩"
bytesData, err := json.Marshal(song)
if err != nil {
fmt.Println(err.Error())
return
}
reader := bytes.NewReader(bytesData)
// url := "http://localhost/echo.php"
url := "http://docu.hxgk.net/uploadfileing/uploadimging"
request, err := http.NewRequest("POST", url, reader)
if err != nil {
fmt.Println(err.Error())
return
}
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
fmt.Println(err.Error())
return
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
return
}
//byte数组直接转成string,优化内存
str := (*string)(unsafe.Pointer(&respBytes))
fmt.Println(*str)
}
func curlShiyan(url string) {
// hsj := httpclient.PostJson()
}