|
@@ -1,28 +1,44 @@
|
|
|
+//go:generate rsrc -ico resource/icon.ico -manifest resource/info.manifest -o main.syso
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
- "fmt"
|
|
|
"io"
|
|
|
"io/ioutil"
|
|
|
"mime/multipart"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
+ "time"
|
|
|
"path"
|
|
|
"strings"
|
|
|
-
|
|
|
+ "zsyy-upload/logger"
|
|
|
"github.com/spf13/viper"
|
|
|
)
|
|
|
|
|
|
+var config *viper.Viper;
|
|
|
+
|
|
|
func main() {
|
|
|
- postFormDataWithMultipartFile()
|
|
|
+ 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 {
|
|
|
- fmt.Println("file read Error!")
|
|
|
- fmt.Printf("%s", err)
|
|
|
+ logger.Info("file read Error!")
|
|
|
+ logger.Errorf("%s", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
@@ -47,32 +63,28 @@ func remove(file string) {
|
|
|
err := os.Remove(file) //删除文件test.txt
|
|
|
if err != nil {
|
|
|
//如果删除失败则输出 file remove Error!
|
|
|
- fmt.Println("file remove Error!", err)
|
|
|
+ logger.Errorf("file remove Error!", err)
|
|
|
} else {
|
|
|
//如果删除成功则输出 file remove OK!
|
|
|
- fmt.Println("file remove OK!", file)
|
|
|
+ logger.Infof("file remove OK!", file)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func postFormDataWithMultipartFile() {
|
|
|
- fmt.Println("=======start=======")
|
|
|
-
|
|
|
- config := viper.New()
|
|
|
- // config.AddConfigPath("./config/")
|
|
|
- // config.SetConfigName("config")
|
|
|
- // config.SetConfigType("yaml")
|
|
|
-
|
|
|
- config.SetConfigFile("./config/config.yaml")
|
|
|
- config.ReadInConfig()
|
|
|
+ logger.Info("=======start========")
|
|
|
|
|
|
postURL := config.GetString("postURL")
|
|
|
pdfPath := config.GetString("pdfPath")
|
|
|
// postURL := "http://localhost:8080/api/tpsUpload"
|
|
|
- fmt.Println("load yml postURL: ", postURL)
|
|
|
- fmt.Println("load yml pdfPath: ", pdfPath)
|
|
|
+ logger.Info("load yml postURL: ", postURL)
|
|
|
+ logger.Info("load yml pdfPath: ", pdfPath)
|
|
|
|
|
|
fileNames, _ := readDir(pdfPath)
|
|
|
- fmt.Println(fileNames)
|
|
|
+ if(fileNames == nil || len(fileNames) == 0){
|
|
|
+ logger.Info("读取文件为空")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ logger.Infof("fileNames==",fileNames)
|
|
|
|
|
|
defer func() {
|
|
|
for _, val := range fileNames {
|
|
@@ -86,15 +98,15 @@ func postFormDataWithMultipartFile() {
|
|
|
for _, val := range fileNames {
|
|
|
file, err := os.Open(pdfPath + val)
|
|
|
if err != nil {
|
|
|
- fmt.Println("err1")
|
|
|
- fmt.Printf("%s", err)
|
|
|
+ logger.Info("err1")
|
|
|
+ logger.Errorf("%s", err)
|
|
|
}
|
|
|
defer file.Close()
|
|
|
fileWrite, _ := bodyWrite.CreateFormFile("files", val)
|
|
|
_, err = io.Copy(fileWrite, file)
|
|
|
if err != nil {
|
|
|
- fmt.Println("err2")
|
|
|
- fmt.Printf("%s", err)
|
|
|
+ logger.Info("err2")
|
|
|
+ logger.Errorf("%s", err)
|
|
|
}
|
|
|
}
|
|
|
bodyWrite.Close() //要关闭,会将w.w.boundary刷写到w.writer中
|
|
@@ -102,16 +114,16 @@ func postFormDataWithMultipartFile() {
|
|
|
// 创建请求
|
|
|
req, err := http.NewRequest(http.MethodPost, postURL, bodyBuf)
|
|
|
if err != nil {
|
|
|
- fmt.Println("err3")
|
|
|
- fmt.Printf("%s", err)
|
|
|
+ logger.Info("err3")
|
|
|
+ logger.Errorf("%s", err)
|
|
|
}
|
|
|
// 设置头
|
|
|
contentType := bodyWrite.FormDataContentType()
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
resp, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
- fmt.Println("err4")
|
|
|
- fmt.Printf("%s", err)
|
|
|
+ logger.Info("err4")
|
|
|
+ logger.Errorf("%s", err)
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
@@ -119,12 +131,12 @@ func postFormDataWithMultipartFile() {
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
if err != nil {
|
|
|
- fmt.Println("err5")
|
|
|
- fmt.Printf("%s", err)
|
|
|
+ logger.Info("err5")
|
|
|
+ logger.Errorf("%s", err)
|
|
|
}
|
|
|
- fmt.Println(string(b))
|
|
|
+ logger.Info(string(b))
|
|
|
|
|
|
// client.CloseIdleConnections()
|
|
|
|
|
|
- fmt.Println("=======end=======")
|
|
|
+ logger.Info("=======end=======")
|
|
|
}
|