Java文件下载

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数组即可下载。


wKiom1XDTcvi36wWAAHiOS1a67Y217.jpg












你可能感兴趣的:(文件下载)