【发布时间】:2021-12-17 13:56:16
【问题描述】:
目前我正在使用以下功能下载文件,我也想从 URL 下载文件夹
任何帮助将不胜感激
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
fileUrl := "http://example.com/file.txt"
err := DownloadFile("./example.txt", fileUrl)
if err != nil {
panic(err)
}
fmt.Println("Downloaded: " + fileUrl)
}
// DownloadFile will download a url to a local file.
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
contentType = resp.Header.Get("Content-Type")
if err != nil {
return err
}
defer resp.Body.Close()
if contentType == "application/octet-stream" {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
}else{
fmt.Println("Requested URL is not downloadable")
}
但我想在 golang 中使用它
【问题讨论】:
-
使用 go query 在 HTML 中查找链接。使用您拥有的代码下载。