背景:
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) <= || len(filename) <= {
//........
}
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))
 
 

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

  1. eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“

    你的位置:首页 > Java编程 > eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“ eclipse发布项目报错:Multipl ...

  2. 链接错误:multiple definition of 'xxx' 问题解决及其原理

    内容借鉴 于CSDN炸鸡叔 错因 截图: “multiple definition of  'head' ” “multiple definition of  'tail' ” 解决过程: 1.首先要 ...

  3. maven pom文件报错:Multiple annotations found at this line 解决方案(转)

    研究maven多模块项目时,因为家里和公司不能同时开发,所以把家里搭建好的项目复制到公司继续研究, 当时家里的电脑搭建好项目之后是没问题的,但是复制到公司的eclipse上之后就看到pom文件出现下面 ...

  4. Xcode10适配——Error:Multiple commands produce

    今天苹果正式推送了iOS12,今天上午就更新了最新的iOS,及Xcode10.这次更新还行,不需要我们对以前的项目紧急修复,大动手术. 用Xcode10跑之前的项目,也就报了一种类型的错误:Multi ...

  5. wordpress插件:multiple post thumbnails(可为文章添加多个特色图片)

    我们经常会给wordpress的文章加上特色图片来实现日志缩略图的需求,但是很多时候一张图片并不能够完美而又全面的表达我们wordpress文章的内容,这时候您可能就会需要这样一个能让wordpres ...

  6. 智汀家庭云-开发指南Golang:设备插件开发

    设备插件模块 开发前先阅读插件设计概要:智汀家庭云-开发指南Golang: 插件模块 使用 plugin-sdk 可以忽略不重要的逻辑,快速实现插件 插件实现 获取sdk go get github. ...

  7. golang:协程安全

    多路复用 Go语言中提供了一个关键字select,通过select可以监听channel上的数据流动.select的用法与switch语法类似,由select开始一个新的选择块,每个选择条件由case ...

  8. Eclipse启动tomcat时报错:Multiple Contexts have a path of "/xxx"

    今天使用Eclipse启动tomcat部署项目时,遇到一个奇怪的错误: Could not publish server configuration for Tomcat v6.0 Server at ...

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

    Multiple Feature Fusion via Weighted Entropy for Visual Tracking ICCV 2015 本文主要考虑的是一个多特征融合的问题.如何有效的进 ...

随机推荐

  1. WebService或HTTP服务端接收请求转发消息到另一个服务端-实现思路

    1.需求结构(WebService) A客户端<->B服务端<->C服务端 说明: a.在B服务端上面添加配置项(1.是否转发消息到C服务端:2.C服务端IP和端口): b.A ...

  2. oracle获取SID

    windows 下查看注册表 开始 输入regedit 查看HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraDb11g_home1\ORACLE_SID就是 lin ...

  3. getopt--parse command line options

    getopt解析命令行选项 getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt - Parse command- ...

  4. maven编译插件版本配置案例

    <!-- Build Settings 构建设置 --> <build> <finalName>${project.artifactId}</finalNam ...

  5. 解决centos7 开机/etc/rc.local 不执行的问题

    最近发现centos7 的/etc/rc.local不会开机执行,于是认真看了下/etc/rc.local文件内容的就发现了问题的原因了 ? 1 2 3 4 5 6 7 8 9 10 11 #!/bi ...

  6. elasticsearch安装与使用(2)-- centos7 安装测试的集群工具elasticsearch head

    elasticsearch-head是elasticsearch(下面称ES)比较普遍使用的可监控.测试等功能的集群管理工具,是由H5编写的单独的网页程序.使用方法网上很多,这里教大家一个超简单安装h ...

  7. [工具使用] 如何访问github

    1.ping github.com,记录github的ip:192.30.252.129 2.找到系统的 hosts文件位置: C:\Windows\System32\drivers\etc\host ...

  8. Spring Cloud是一系列框架的有序集合

    Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据监控等,都可以用 ...

  9. XMPP客户端

    1. Strophe.js 2. Converse.js

  10. Tomcat6连接数设置:permsize

    Tomcat6默认配置,在后台一阵全点击服务器就报废了,查了一下就要是PERMSIZE默认值过小造成(16-64) TOMCAT_HOME/bin/catalina.sh 添加一行:JAVA_OPTS ...