123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //go:generate rsrc -ico resource/icon.ico -manifest resource/info.manifest -o main.syso
- package main
- import (
- "bytes"
- "io"
- "io/ioutil"
- "mime/multipart"
- "net/http"
- "os"
- "time"
- "path"
- "strings"
- "zsyy-upload/logger"
- "github.com/spf13/viper"
- )
- var config *viper.Viper;
- func main() {
- config = viper.New()
- config.SetConfigFile("./config/settings.yaml")
- config.ReadInConfig()
- sleeptime := config.GetInt("settings.sleeptime")
-
- logger.Init()
- i := 1
- for i<10 {
- postFormDataWithMultipartFile()
- time.Sleep(time.Second * time.Duration(sleeptime))
- }
-
- }
- func readDir(dirname string) ([]string, error) {
- infos, err := ioutil.ReadDir(dirname)
- if err != nil {
- logger.Info("file read Error!")
- logger.Errorf("%s", err)
- return nil, err
- }
- // names := make([]string, len(infos))
- var names []string
- for _, info := range infos {
- fileType := path.Ext(info.Name())
- if len(fileType) != 0 {
- fileType = strings.ToLower(fileType)
- if fileType == ".pdf" {
- names = append(names, info.Name())
- }
- }
- // names[i] = info.Name()
- }
- return names, nil
- }
- func remove(file string) {
- err := os.Remove(file) //删除文件test.txt
- if err != nil {
- //如果删除失败则输出 file remove Error!
- logger.Errorf("file remove Error!", err)
- } else {
- //如果删除成功则输出 file remove OK!
- logger.Infof("file remove OK!", file)
- }
- }
- func postFormDataWithMultipartFile() {
- logger.Info("=======start========")
- postURL := config.GetString("postURL")
- pdfPath := config.GetString("pdfPath")
- // postURL := "http://localhost:8080/api/tpsUpload"
- logger.Info("load yml postURL: ", postURL)
- logger.Info("load yml pdfPath: ", pdfPath)
- fileNames, _ := readDir(pdfPath)
- if(fileNames == nil || len(fileNames) == 0){
- logger.Info("读取文件为空")
- return
- }
- logger.Infof("fileNames==",fileNames)
- defer func() {
- for _, val := range fileNames {
- remove(pdfPath + val)
- }
- }()
- client := http.Client{}
- bodyBuf := &bytes.Buffer{}
- bodyWrite := multipart.NewWriter(bodyBuf)
- for _, val := range fileNames {
- file, err := os.Open(pdfPath + val)
- if err != nil {
- logger.Info("err1")
- logger.Errorf("%s", err)
- }
- defer file.Close()
- fileWrite, _ := bodyWrite.CreateFormFile("files", val)
- _, err = io.Copy(fileWrite, file)
- if err != nil {
- logger.Info("err2")
- logger.Errorf("%s", err)
- }
- }
- bodyWrite.Close() //要关闭,会将w.w.boundary刷写到w.writer中
- // 创建请求
- req, err := http.NewRequest(http.MethodPost, postURL, bodyBuf)
- if err != nil {
- logger.Info("err3")
- logger.Errorf("%s", err)
- }
- // 设置头
- contentType := bodyWrite.FormDataContentType()
- req.Header.Set("Content-Type", contentType)
- resp, err := client.Do(req)
- if err != nil {
- logger.Info("err4")
- logger.Errorf("%s", err)
- }
- defer resp.Body.Close()
- // defer req.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- logger.Info("err5")
- logger.Errorf("%s", err)
- }
- logger.Info(string(b))
- // client.CloseIdleConnections()
- logger.Info("=======end=======")
- }
|