背景:
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. springboot集成jdbcTemplate

    这里使用springboot自带的jdbcTemplate连接mysql数据库 1. 添加依赖包 <!-- jdbc --> <dependency> <groupId& ...

  2. Memcached内存分配及使用问题

    在启动memcached的时候可以加-f参数和-n参数.-f指定各slab里面chunk大小的变化比例,默认1.25,-n指定slab里面chunk大小从多少开始.使用memcache_add($me ...

  3. Android学习之两款下拉刷新库分享

    昨天没有写博客.心里非常罪过呀,今天给大家写两种比較常见的下拉刷新的用法.一款是SwipeRefreshLayout,一款是CircleRefreshLayout. SwipeRefreshLayou ...

  4. plsql programming 01 plsql概述

    授权 从 oracle 8i 开始, oracle 用通过提供 authid 子句为 pl/sql 的执行授权模型, 这样我们可以选择使用 authid current_user(调用者权限)来执行这 ...

  5. 【BZOJ】1637: [Usaco2007 Mar]Balanced Lineup(前缀和+差分+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1637 很神思想.. 前缀和应用到了极点... 我们可以发现当数量一定时,这个区间最前边的牛的前边一个 ...

  6. 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头(特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1607 其实题目描述不清楚,应该是 别人拿的数能整除自己拿的数 数据范围很大,n<=100000 ...

  7. 自定义 httpHandler 配置

    开发 环境 vs2010.asp.net 4.0 时 写在<system.web>  ,<httpHandlers>节点下 部署 环境Windows server 2007 , ...

  8. hdu 4739(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4739 思路:状态压缩. #include<iostream> #include<cs ...

  9. golang中context包学习

    摘要 go语言中goroutine之间的关联关系,缺乏维护,在erlang中有专门的机制来保障新开仟程的生命周期, 在go语言中,只能通过channel + select来实现,但不够直观,感觉很绕. ...

  10. 安装PHP扩展-----phpredis

    一.redis介绍 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它支持存储的value类型相 ...