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.
 
 
 
 
 

157 lines
3.7 KiB

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()
}