main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //go:generate rsrc -ico resource/icon.ico -manifest resource/info.manifest -o main.syso
  2. package main
  3. import (
  4. "bytes"
  5. "io"
  6. "io/ioutil"
  7. "mime/multipart"
  8. "net/http"
  9. "os"
  10. "time"
  11. "path"
  12. "strings"
  13. "zsyy-upload/logger"
  14. "github.com/spf13/viper"
  15. )
  16. var config *viper.Viper;
  17. func main() {
  18. config = viper.New()
  19. config.SetConfigFile("./config/settings.yaml")
  20. config.ReadInConfig()
  21. sleeptime := config.GetInt("settings.sleeptime")
  22. logger.Init()
  23. i := 1
  24. for i<10 {
  25. postFormDataWithMultipartFile()
  26. time.Sleep(time.Second * time.Duration(sleeptime))
  27. }
  28. }
  29. func readDir(dirname string) ([]string, error) {
  30. infos, err := ioutil.ReadDir(dirname)
  31. if err != nil {
  32. logger.Info("file read Error!")
  33. logger.Errorf("%s", err)
  34. return nil, err
  35. }
  36. // names := make([]string, len(infos))
  37. var names []string
  38. for _, info := range infos {
  39. fileType := path.Ext(info.Name())
  40. if len(fileType) != 0 {
  41. fileType = strings.ToLower(fileType)
  42. if fileType == ".pdf" {
  43. names = append(names, info.Name())
  44. }
  45. }
  46. // names[i] = info.Name()
  47. }
  48. return names, nil
  49. }
  50. func remove(file string) {
  51. err := os.Remove(file) //删除文件test.txt
  52. if err != nil {
  53. //如果删除失败则输出 file remove Error!
  54. logger.Errorf("file remove Error!", err)
  55. } else {
  56. //如果删除成功则输出 file remove OK!
  57. logger.Infof("file remove OK!", file)
  58. }
  59. }
  60. func postFormDataWithMultipartFile() {
  61. logger.Info("=======start========")
  62. postURL := config.GetString("postURL")
  63. pdfPath := config.GetString("pdfPath")
  64. // postURL := "http://localhost:8080/api/tpsUpload"
  65. logger.Info("load yml postURL: ", postURL)
  66. logger.Info("load yml pdfPath: ", pdfPath)
  67. fileNames, _ := readDir(pdfPath)
  68. if(fileNames == nil || len(fileNames) == 0){
  69. logger.Info("读取文件为空")
  70. return
  71. }
  72. logger.Infof("fileNames==",fileNames)
  73. defer func() {
  74. for _, val := range fileNames {
  75. remove(pdfPath + val)
  76. }
  77. }()
  78. client := http.Client{}
  79. bodyBuf := &bytes.Buffer{}
  80. bodyWrite := multipart.NewWriter(bodyBuf)
  81. for _, val := range fileNames {
  82. file, err := os.Open(pdfPath + val)
  83. if err != nil {
  84. logger.Info("err1")
  85. logger.Errorf("%s", err)
  86. }
  87. defer file.Close()
  88. fileWrite, _ := bodyWrite.CreateFormFile("files", val)
  89. _, err = io.Copy(fileWrite, file)
  90. if err != nil {
  91. logger.Info("err2")
  92. logger.Errorf("%s", err)
  93. }
  94. }
  95. bodyWrite.Close() //要关闭,会将w.w.boundary刷写到w.writer中
  96. // 创建请求
  97. req, err := http.NewRequest(http.MethodPost, postURL, bodyBuf)
  98. if err != nil {
  99. logger.Info("err3")
  100. logger.Errorf("%s", err)
  101. }
  102. // 设置头
  103. contentType := bodyWrite.FormDataContentType()
  104. req.Header.Set("Content-Type", contentType)
  105. resp, err := client.Do(req)
  106. if err != nil {
  107. logger.Info("err4")
  108. logger.Errorf("%s", err)
  109. }
  110. defer resp.Body.Close()
  111. // defer req.Body.Close()
  112. b, err := ioutil.ReadAll(resp.Body)
  113. if err != nil {
  114. logger.Info("err5")
  115. logger.Errorf("%s", err)
  116. }
  117. logger.Info(string(b))
  118. // client.CloseIdleConnections()
  119. logger.Info("=======end=======")
  120. }