Golang从URL下载文件时无法获取完整文件

Golang从URL下载文件时无法获取完整文件 我有代码可以从github.com下载JSON文件,但当我检查本地机器上的文件时,发现文件附加了不必要的内容。我只需要原始文件,如该链接所示:https://raw.githubusercontent.com/f5devcentral/f5-tetration/master/scripts/Tetration_TCP_L4_ipfix.json

如果在本地机器上打开文件,开头会显示类似这样的内容…

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  <link rel="dns-prefetch" href="https://assets-cdn.github.com">
  <link rel="dns-prefetch" href="https://avatars0.githubusercontent.com">
  <link rel="dns-prefetch" href="https://avatars1.githubusercontent.com">
  <link rel="dns-prefetch" href="https://avatars2.githubusercontent.com">
  <link rel="dns-prefetch" href="https://avatars3.githubusercontent.com">
  <link rel="dns-prefetch" href="https://github-cloud.s3.amazonaws.com">
  <link rel="dns-prefetch" href="https://user-images.githubusercontent.com/">

以下是代码:

fileTCPexists := fileTCPexists() // returns true or false
	if fileTCPexists {
		fmt.Println(" TCP File exists on your local machine")

	} else {
		fmt.Println(" irule does not exists on local machine ..... getting from github")
		downloadTCPiruleFromGithub()
	}

func downloadTCPiruleFromGithub() bool {
	fmt.Println(" Downloading from github ........")
	fileUrl := "https://github.com/f5devcentral/f5-tetration/blob/master/scripts/Tetration_TCP_L4_ipfix.json"

	err := DownloadFile("irules/Tetration_TCP_L4_ipfix.json", fileUrl)
	if err != nil {
		panic(err)
	}
	return true
}

  
//DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {

	// Create the file
	out, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Get the data
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	if err != nil {
		return err
	}

	return nil
}

更多关于Golang从URL下载文件时无法获取完整文件的实战教程也可以访问 https://www.itying.com/category-94-b0.html

5 回复

感谢,这个方法有效

更多关于Golang从URL下载文件时无法获取完整文件的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


christophberger:

http://raw.githubusercontent.com

“感谢,这个方法有效”

scshitole:

fileUrl := "https://github.com/f5devcentral/f5-tetration/blob/master/scripts/Tetration_TCP_L4_ipfix.json"

这是经过HTML化的URL,你为什么不使用在介绍中使用的原始URL呢?

尝试使用 raw.githubusercontent.com 的 URL 替代 github.com 的 URL。

指向 github.com 的 URL,即使以文件名结尾,也总是会加载包含该文件的完整网页。(将您代码示例中的 URL 粘贴到浏览器地址栏中即可明白我的意思。)

问题在于你使用了错误的URL。GitHub仓库页面的URL(如https://github.com/f5devcentral/f5-tetration/blob/master/scripts/Tetration_TCP_L4_ipfix.json)返回的是包含文件内容的HTML页面,而不是原始JSON文件。

需要使用GitHub的原始文件URL格式:https://raw.githubusercontent.com/用户名/仓库名/分支名/文件路径

修正后的代码:

func downloadTCPiruleFromGithub() bool {
    fmt.Println(" Downloading from github ........")
    // 使用raw.githubusercontent.com获取原始文件
    fileUrl := "https://raw.githubusercontent.com/f5devcentral/f5-tetration/master/scripts/Tetration_TCP_L4_ipfix.json"

    err := DownloadFile("irules/Tetration_TCP_L4_ipfix.json", fileUrl)
    if err != nil {
        panic(err)
    }
    return true
}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
    // Create the file
    out, err := os.Create(filepath)
    if err != nil {
        return err
    }
    defer out.Close()

    // Get the data
    resp, err := http.Get(url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    // Check for successful HTTP status
    if resp.StatusCode != http.StatusOK {
        return fmt.Errorf("bad status: %s", resp.Status)
    }

    // Write the body to file
    _, err = io.Copy(out, resp.Body)
    if err != nil {
        return err
    }

    return nil
}

主要修改:

  1. 将URL从https://github.com/f5devcentral/f5-tetration/blob/master/scripts/Tetration_TCP_L4_ipfix.json改为https://raw.githubusercontent.com/f5devcentral/f5-tetration/master/scripts/Tetration_TCP_L4_ipfix.json
  2. 在DownloadFile函数中添加了HTTP状态码检查,确保下载成功

这样修改后,下载的文件将是纯JSON内容,不会包含HTML包装。

回到顶部