我有一個客戶端發出下載文件的請求,Web 服務器將此請求轉發到實際保存該文件的資源服務器。從資源服務器返回的 *http.Response 具有 Body io.ReaderCloser 從資源服務器流式傳輸文件內容。但是我現在想開始將其寫入來自客戶端的原始 http.ResponseWriter 中。查看 http.ResponseWriter 接口,它只包含一個 Write 方法,它需要一個字節片,這讓我認為將文件內容返回給客戶端的唯一方法是將 Body io.ReaderCloser 讀入緩沖區,然后將其放入 http.ResponseWriter 的 Write 方法中。我不想這樣做,因為這非常低效,而且通過我的 Web 服務器流式傳輸它會好得多。這可能嗎?這是一些代碼來說明:getFile() *http.Response { //make a request to resource server and return the response object}// handle request from clienthttp.HandleFunc("/getFile", func(w http.ResponseWriter, r *http.Request){ res := getFile() //how can I stream res.Body into w without buffering ?})
- 1 回答
- 0 關注
- 347 瀏覽
添加回答
舉報
0/150
提交
取消