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