-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc.go
More file actions
62 lines (50 loc) · 995 Bytes
/
c.go
File metadata and controls
62 lines (50 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @Date: 2021-07-16 17:43:55
* @LastEditors: jaxiu
* @LastEditTime: 2021-07-22 15:03:47
* @FilePath: /test/gin/c.go
*/
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
var file = flag.String("f", "", "upload filepath+filename")
var domain = flag.String("d", "up.jaxiu.cn", "upload host")
func main() {
flag.Parse()
fmt.Println("file:", *file)
c := http.DefaultClient
f, err := os.Open(*file)
if err != nil {
panic(err)
}
defer f.Close()
body := &bytes.Buffer{}
w := multipart.NewWriter(body)
defer w.Close()
part, err := w.CreateFormFile("file", *file)
if err != nil {
panic(err)
}
io.Copy(part, f)
ct := w.FormDataContentType()
w.Close()
resp, err := c.Post("http://"+*domain+"/up", ct, body)
if err != nil {
panic(err)
}
fmt.Printf("resp: %+v\n", resp)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("resp body: %+v\n", string(b))
defer resp.Body.Close()
}