golang: multiple http.writeHeader calls

背景:

golang的http服务,读取文件,提供给client下载时候。

出现 multiple http.writeHeader calls 错误。

func DownloadFile(w http.ResponseWriter, r *http.Request, sequence uint64, userid string) {
    userkey := userid
    filename := r.FormValue("filename")
    if len(userkey) <= 0 || len(filename) <= 0 {
        //........
    }
    fp := fmt.Sprintf("%s%s/%s", g_sc.Rootdir, userkey, filename)
    fd, err := os.Open(fp)
    if err == nil {
        defer fd.Close()
        buf, err := ioutil.ReadAll(fd)
        if err != nil {
            //.........
        } else {
            size := len(buf)
            w.Header().Add("Content-Length", fmt.Sprintf("%d", size))
            fmt.Fprint(w, string(buf))
        }
    }
}

问题出现在这几行代码:

size := len(buf)
w.Header().Add("Content-Length", fmt.Sprintf("%d", size))
fmt.Fprint(w, string(buf))

可以做如下修改:

buf := new(bytes.Buffer)
buf.Write(...)

if err {
  // you can use http.Error here, no response has been written yet
  http.Error(w, err.String, http.StatusInternalServerError)
  return
}

if err := buf.WriteTo(w); err != nil {
  log.Printf("WriteTo: %v", err)
  // you can not use http.Error here anymore. So just log the message (e.g. "broken pipe...")
}

或者注释这两行代码

//size := len(buf)
 //w.Header().Add("Content-Length", fmt.Sprintf("%d", size))
fmt.Fprint(w, string(buf))

时间: 2024-12-30 09:06:28

golang: multiple http.writeHeader calls的相关文章

QT编译错误: multiple definition of `qMain(int, char**)&#39;

QT使用过程中来回添加修改代码,结果出现了编译错误:error: multiple definition of `qMain(int, char**)' 一直看我的源文件是都哪里有错误,最后发现是在pro文件中出的问题,频繁的添加以及移除文件,导致HEADERS以及SOURCES中会重复添加 解决办法:把重复项删除! QT编译错误: multiple definition of `qMain(int, char**)' 原文地址:https://www.cnblogs.com/MakeView6

链接错误:multiple definition of &#39;xxx&#39; 问题解决及其原理

内容借鉴 于CSDN炸鸡叔 错因 截图: “multiple definition of  'head' ” “multiple definition of  'tail' ” 解决过程: 1.首先要  区别  变量的定义 和 声明 声明是向 编译器介绍名字---标识符,它告诉编译器“这个函数或变量 在哪儿可找到,模样”: 而定义是说: “在这里建立变量或函数”,即为变量或函数 分配存储空间. 对于变量,编译器确定变量的大小,然后在内存中开辟空间来保存其数据. 对于函数,编译器会生成代码,这些代

Android Studio:Multiple dex files define Landroid/support/annotation/AnimRes

最近真的比较忙,一不小心博客又荒了两个月.从今天起,决定重返csdn,多多纪录和分享.先从一个最近被折磨的死去活来的问题. 由于升级了V4包,就一直报这个问题: com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes; 每次都要clean一下,然后才能编译过,光这个过程就浪费了4/5分钟.出现这个问题是因为最新的v4包(compile 'com.android.supp

Golang:slice之append时原数组发生变化的问题

使用append可以在slice之后追求元素,例如 nums:=[]int{1,2,3} result:=append(nums,4) fmt.Println(result) 这段代码很简单,输出result的值为:[1 2 3 4] 问题在于,进行这种操作时,原来的slice(即nums)所基于的数组的值会不会发生变化呢?在Golang中,如果有多个slice基于了同一个数组,则这些slice的数据是共享的(而不是每个slice复制一份).也就说,如果改变了数组的内容,则基于它的所有slice

问题解决: multiple definition of XXX

在编译程序的时候,遇到了一个问题,花点时间记录一下: 在Qt中创建一个类后,一般是先在.h文件中声明变量与函数,然后在对应的.cpp文件中对各个函数进行定义,这在往常使用中没有任何问题,今天在使用Qt时,在各.cpp源文件编译时出现了许多multiple definition of XXX的错误. 搜寻了网上一些资料,总算是解决multiple definition of 的方法: 问题解决方法之一: 根据网上的解释,multiple definition of 原因是因为在多次包含global

论文笔记之:Multiple Feature Fusion via Weighted Entropy for Visual Tracking

Multiple Feature Fusion via Weighted Entropy for Visual Tracking ICCV 2015 本文主要考虑的是一个多特征融合的问题.如何有效的进行加权融合,是一个需要解决的问题.本文提出一种新的 data-adaptive visual tracking approach 通过 weighted entropy 进行多特征融合.并非像许多方法所做的简单的链接在一起的方法,本文采用加权的 entropy 来评价目标状态和背景状态之间的区分性,

eclipse报错:Multiple annotations found at this line: - String cannot be resolved to a type解决方法实测

Multiple annotations found at this line:- String cannot be resolved to a type- The method getContextPath() from the type HttpServletRequest refers to the missing type String 解决办法: 首先 右击该项目 - Build Path - Configure Build Path , 在 Libraries 选项下,会发现有个出错

论文笔记之:MULTIPLE OBJECT RECOGNITION WITH VISUAL ATTENTION

 Multiple Object Recognition With Visual Attention Google DeepMind  ICRL 2015 s

Golang:测试map是否存在

请看这个url:http://www.du52.com/text.php?id=561 if v, ok := m1["a"]; ok { fmt.Println(v) } else { fmt.Println("Key Not Found") }