当前位置: 首页 > 工具软件 > go-torrent > 使用案例 >

Go-通过Http下载文件

梁存
2023-12-01
// DownFile 通过Http下载文件
func DownFile(imgUrl, projectID, uuid string) (string, error) {
	res, httpErr := http.Get(imgUrl)
	if httpErr != nil {
		return "Http Error", httpErr
	}

	defer res.Body.Close()
	defineConfig := global.GetConfig("define")
	imgPath := defineConfig.GetString("AVATAR_PATH")
	imageFilePath := imgPath + projectID + "-" + uuid + ".jpg"
	reader := bufio.NewReaderSize(res.Body, 64*1024) // 获得reader对象
	file, createErr := os.Create(imageFilePath)
	if createErr != nil {
		return "Create Error", createErr
	}

	writer := bufio.NewWriter(file) // 获得writer对象
	_, copyErr := io.Copy(writer, reader)
	if copyErr != nil {
		return "Copy Error", copyErr
	}

	return imageFilePath, nil
}

 类似资料: