How to check whether a URL is downloadable or not in golang?

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

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是否可下载的方法。希望对某人有所帮助 How to check whether a URL is downloadable or not in golang?

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 How to check whether a URL is downloadable or not in golang?

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>



huangapple
  • 本文由 发表于 2021年10月28日 15:00:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/69749564.html
匿名

发表评论

匿名网友

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

确定