Spring MVC 下载csv文件
Spring MVC 下载csv文件
下载代码:
@RequestMapping(value="/MmsDeatail_exportMmsDetail", method= RequestMethod.GET)
public void exportMmsDetail(HttpServletRequest request, HttpServletResponse response)
{
String reportName = "export_outgoing.csv";
response.reset();
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Encoding", "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + reportName);
OutputStream outputStream = null;
outputStream = response.getOutputStream();
outputStream.write(0xEF); // 1st byte of BOM
outputStream.write(0xBB);
outputStream.write(0xBF); // last byte of BOM
// now get a PrintWriter to stream the chars.
PrintWriter os = new PrintWriter(new OutputStreamWriter(outputStream,"UTF-8"));
StringBuffer sb = new StringBuffer();
sb.append("Sender Number,Campaign Title,Requested Time,Sent Time,Size,Status,Recipients");
sb.append("\n");sb.append("85245699654" + ",");
os.print(sb);
os.flush();
os.close();
}
注意事项:,请求的方式不能是post,只能是get。
即@RequestMapping(value="/MmsDeatail_exportMmsDetail", method= RequestMethod.GET)