本文主要是介绍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();
}
这篇关于BufferedOutputStream下载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!