golang download file and follow redirects

huangapple go评论165阅读模式
英文:

golang download file and follow redirects

问题

你想要从重定向的URL下载文件,并将文件名返回到变量filename中。你的实际代码如下:

  1. package main
  2. import (
  3. "os"
  4. "net/http"
  5. "io"
  6. "path/filepath"
  7. )
  8. func downloadFile(filepath string, url string) (string, error) {
  9. // Create the file
  10. out, err := os.Create(filepath)
  11. if err != nil {
  12. return "", err
  13. }
  14. defer out.Close()
  15. // Get the data
  16. resp, err := http.Get(url)
  17. if err != nil {
  18. return "", err
  19. }
  20. defer resp.Body.Close()
  21. // Writer the body to file
  22. _, err = io.Copy(out, resp.Body)
  23. if err != nil {
  24. return "", err
  25. }
  26. // Get the real filename
  27. realFilename := filepath.Base(resp.Request.URL.Path)
  28. return realFilename, nil
  29. }
  30. func main() {
  31. var filename string = "urls.txt"
  32. var url1 string = "http://94.177.247.162:5000/random"
  33. realFilename, err := downloadFile(filename, url1)
  34. if err != nil {
  35. panic(err)
  36. }
  37. // Use the real filename
  38. filename = realFilename
  39. // Print the filename
  40. println(filename)
  41. }

你可以通过在downloadFile函数中添加一些代码来获取实际的文件名。使用filepath.Base(resp.Request.URL.Path)可以从URL的路径中提取文件名。然后,你可以将实际的文件名赋值给filename变量,并打印出来。

英文:

i want to download file from redirect url, and return filename in variable filename, my actual code is :

  1. package main
  2. import (
  3. "os"
  4. "net/http"
  5. "io"
  6. )
  7. func downloadFile(filepath string, url string) (err error) {
  8. // Create the file
  9. out, err := os.Create(filepath)
  10. if err != nil {
  11. return err
  12. }
  13. defer out.Close()
  14. // Get the data
  15. resp, err := http.Get(url)
  16. if err != nil {
  17. return err
  18. }
  19. defer resp.Body.Close()
  20. // Writer the body to file
  21. _, err = io.Copy(out, resp.Body)
  22. if err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. func main() {
  28. var filename string = "urls.txt"
  29. var url1 string = "http://94.177.247.162:5000/random"
  30. downloadFile(filename, url1)
  31. }

how i can change my code to return real name of file downloaded in variable filename.

example output :

  1. urlix.txt

答案1

得分: 2

重定向应该是默认跟随的。你可以使用以下代码获取访问的最终URL来获取下载链接:

  1. finalURL := resp.Request.URL.String()

URL的最后一部分是远程文件的文件名,所以可以使用以下代码来获取文件名:

  1. parts := strings.Split(finalURL, "/")
  2. filename := parts[len(parts)-1]
英文:

Redirects should be followed by default. You can get the final url visited to aquire the download using

finalURL := resp.Request.URL.String()

The last part of the url is your filename on the remote so something like this should work


parts := strings.Split(finalURL, "/")
filename := parts[len(parts)-1]

huangapple
  • 本文由 发表于 2017年9月5日 19:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/46054094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定