码迷,mamicode.com
首页 > 编程语言 > 详细

Java文件下载

时间:2015-08-06 22:40:33      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:文件下载

csv格式文件实际为txt文本文件,可以右键选择用notepad++查看

//HttpServletResponse response...
try {
	StringBuilder sb = new StringBuilder();
	//sb为文本内容...
	byte[] b = sb.toString().getBytes();  
	response.setCharacterEncoding("utf-8");  
	String filename = "";
	if (operation_type != null && operation_type.equals("RELEASE")) {
		filename = "应用发布记录";
	} else if (operation_type != null && operation_type.equals("OPS")) {
		filename = "应用运维记录";
	}
	filename=URLEncoder.encode(filename,"utf-8"); //解决中文文件名下载后乱码的问题 
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss"); 
	response.setHeader("Content-Disposition","attachment; filename=" + filename+ sdf.format(new Date())+".csv");  
	//获取响应报文输出流对象  
	//javax.servlet.ServletOutputStream
	ServletOutputStream  out =response.getOutputStream();  
	//输出  
	out.write(b);  
	out.flush();  
	out.close();  
} catch (IOException e) {

}


这里以下载文本文件为示例,实际上下载的文件可以是任何格式的。只要将要下载的数据转成byte数组即可下载。


技术分享












本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1682386

Java文件下载

标签:文件下载

原文地址:http://shamrock.blog.51cto.com/2079212/1682386

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!