英文:
How to check whether a URL is downloadable or not in golang?
问题
我正在尝试从URL下载文件到本地文件。
我想测试一下请求的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将URL下载到本地文件。
func DownloadFile(filepath string, url string) error {
// 获取数据
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// 创建文件
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// 将内容写入文件
_, err = io.Copy(out, resp.Body)
return err
}
英文:
I'm trying to download a file fro the url to a local file.
I wanted to test whether the requesting url is only file, if it is not a file it should return bad request
Any help could be appreciated
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)
if err != nil {
return err
}
defer resp.Body.Close()
// 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
}
答案1
得分: 0
以下是检查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将URL下载到本地文件。
func DownloadFile(filepath string, url string) error {
// 获取数据
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" {
// 创建文件
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// 将内容写入文件
_, err = io.Copy(out, resp.Body)
return err
} else {
fmt.Println("请求的URL无法下载")
return nil
}
}
英文:
Below is the way to check whether the URL is downloadable or not. Hope this helps someone
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")
return nil
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论