BufferedOutputStream下载

例:

public void demoexport(HttpServletRequest request,HttpServletResponse response){
OutputStream os = null;
SyspleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  //设置日期格式
//sdf.format(new date());  
int random = (int) (Math.random() * 1000) //获取0-1000之间的随机数,然后强转为整形
String fileName = "交易导出_"+sdf.format(new date())+"_"+random+".csv"; 
request.setCharacterEncoding("UTF-8"); //设置输入流的格式
os = response.getOutputStrem(); //取得输出流
response.reset(); //清空输出流
response.setHeader("Content-disposition","attachment;filename="+new String(fileName.getBytes("GBK"),"ISO8859-1"));//设置导出文件的文件名
response..setContentType("application/msexcel;charset=UTF-8"); //定义输出类型
list...     //这是获取要下载的内容的集合,因为每个人的都不同,所以没写,大家懂就行
BufferedOutputStream bos = new BufferedOutputStrem(os);  //相当于一个缓冲,最后还是会到outputstream里面去的,详情请见             http://blog.sina.com.cn/s/blog_735065f90100pv3g.html
String title = "代理商编号,代理商名称,商户编号,商户名称,商户类别\n";
bos.write(title.getBytes("GBK"))
Iterator<Map<String, Object>> it = list.iterator();
while(it.hasNext()){
Map<String, Object> map =it.next();
StringBuffer content = new StringBuffer();
content.append("\t").append(StringUtil.ifEmptyThen(map.get("agent_no"), "无")).append(",");
content.append("\t").append(StringUtil.ifEmptyThen(map.get("agent_name"), "无")).append(",");
content.append("\t").append(StringUtil.ifEmptyThen(map.get("merchant_no"), "无")).append(",");
content.append("\t").append(StringUtil.ifEmptyThen(map.get("merchant_short_name"), "无")).append(",");
content.append("\t").append(StringUtil.ifEmptyThen(map.get("type_name"), "无")).append("\n");
bos.write(content.toString().getBytes("GBK"));
}

bos.flush();
bos.close();
os.close();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值