标签:文件下载
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
标签:文件下载
原文地址:http://shamrock.blog.51cto.com/2079212/1682386