dthdlv9777 2018-07-10 07:17
浏览 37
已采纳

将原始Goreq.body响应附加到文件

Hi I would like to append a downloaded file raw response to a file. Supposed that I'm trying to download a 1GB file and download process will be done in multiple request. Let say 100mb per request. Now this all the 10 parts should be put into one single file to be read. I don't know how to achieve this. I'm using GOREQ to request this resource and the raw data is in Goreq.body format. Below is my sample code

newFile := "./newfile.gz"
out, err := os.Create(newFile)
defer out.close()  

if err != nil {
    fmt.Println("Failed to download file")
    return
}

for i := 0; i < 10; i++ {
    // This will return the items in part of 100mb/part. 
    // GetRawDataFromEbay returns a goreq.body(raw) as its response
    response, _ = GetRawDataFromEbay(10485760) 

    out.Write(response) // This does not work
}

I know Write accept []byte as parameter. But how can I append the retrieve goreq.body raw data to a file?

  • 写回答

2条回答 默认 最新

  • dongwen2794 2018-07-13 06:37
    关注

    Ok found the answer it seems that I don't need to do anything fancy. I just have to use io.copy and that's it. Just for reference for others having the same issue below is my code

    newFile := "./newfile.gz"
    out, err := os.Create(newFile)
    defer out.close()   
    
    if err != nil {
       fmt.Println("Failed to download file")
       return
    }
    
    for i := 0; i < 10; i++ {
        // This will return the items in part of 100mb/part. 
        // GetRawDataFromEbay returns a goreq.body(raw) as its response
        response, _ = GetRawDataFromEbay(10485760) 
        io.Copy(file, response) // Where file is an instance of *os.File
    }
    

    So basically if the Goreq.body response is in by part when you copy the different response (in part) in the same file it will append it automatically

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分