main.go 2.7 KB

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