背景:
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. 对redis深入理解

    1.Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet.如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog. ...

  2. xcode6 dyld_sim is not owned by root

    如果运行复制过来的xcode可能会这个提示,xcode6 dyld_sim is not owned by root解决方法打开终端 输入sudo xcode-select -switch /Appl ...

  3. pictureBox

    private void pictureBox1_Click(object sender, EventArgs e) { openFileDialog1.Filter="*.jpg|*.jp ...

  4. SSL/TLS协议运行机制的概述_转

    转自:SSL/TLS协议运行机制的概述 作者: 阮一峰 日期: 2014年2月 5日 互联网的通信安全,建立在SSL/TLS协议之上. 本文简要介绍SSL/TLS协议的运行机制.文章的重点是设计思想和 ...

  5. JavaScript概述.pdf

    第1章 JavaScript概述 第2章 使用JavaScript 第3章 语法.关键保留字及变量 第4章 数据类型 第5章 运算符 第6章 流程控制语句 第7章 函数 //没有参数的函数 funct ...

  6. Scala中Stream的应用场景及其实现原理

    欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/10/23/scala-stream-application-scenario-and-how-its-imple ...

  7. Servlet中Request的getAttribute getParameter 区别

    1.从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据. request.getParameter()方法返回Str ...

  8. php -- 魔术方法 之 获取属性:__get()

    属性重载:当访问一个不存在或者权限不够的属性的时候,能够触发一系列的魔术方法,就叫做属性重载 __get():获取不存在或者权限不够的属性的时候会自动触发 <?php header('Conte ...

  9. php -- ziparchive::open创建zip压缩文件

    语法: mixed ZipArchive::open ( string $filename [, int $flags ] ) 参数: filename:创建的zip的文件名 flags: ZIPAR ...

  10. 【NOIP模拟题】Permutation(dp+高精度)

    首先我们可以这样想: 设状态f[i, j]表示1-i序列有j个'<'的方案数 那么考虑转移 因为i比i-1大,所以可以考虑从i-1来转移.首先i是要插入1-i-1这个序列的,所以我们可以思考插入 ...