/
fileSendUtil.go
72 lines (67 loc) · 1.54 KB
/
fileSendUtil.go
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
63
64
65
66
67
68
69
70
71
72
// 文件HTTP传输方法
package gutil
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
/**
文件发送处理方法
创建人:邵炜
创建时间:2016年11月29日15:37:06
输入参数:文件路径
输出参数:错误对象00001*/
func HttpSendFile(sendHttpUrl, filePathStr, uploadFile string) error {
_, err := os.Stat(filePathStr)
if err != nil {
return err
}
extraParams := map[string]string{
"title": "My Document",
"author": "Matt Aimonetti",
"description": "A document with all the Go programming language secrets",
}
request, err := newfileUploadRequest(sendHttpUrl, extraParams, uploadFile, filePathStr)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return err
} else {
body := &bytes.Buffer{}
_, err := body.ReadFrom(resp.Body)
if err != nil {
return err
}
}
return nil
}