下载文件

  1. func Download(imageURL string) {
  2. imgPath := "D:\\temp\\one\\"
  3. fileName := path.Base(imageURL)
  4. res, err := http.Get(imageURL)
  5. if err != nil {
  6. fmt.Println("A error occurred!")
  7. return
  8. }
  9. defer res.Body.Close()
  10. // 获得get请求响应的reader对象
  11. reader := bufio.NewReaderSize(res.Body, 32*1024)
  12. file, err := os.Create(imgPath + fileName + ".jpg")
  13. if err != nil {
  14. panic(err)
  15. }
  16. // 获得文件的writer对象
  17. writer := bufio.NewWriter(file)
  18. written, _ := io.Copy(writer, reader)
  19. fmt.Printf("Total length: %d", written)
  20. }