记录一下最近写的将数据库中现有的数据下载功能,第一次写有点蒙圈
前端页面:
controller层:
@RequestMapping(value=“export”,method = RequestMethod.POST)
public Result expor(HttpServletRequest request, HttpServletResponse response){
try{
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日”);
String fileName = “appList”+sdf.format(new Date())+".txt";
ServletOutputStream out = null;
request.setCharacterEncoding(“UTF-8”);
response.setContentType(“application/octet-stream”);
//清除response中的缓存
response.reset();
//Content-Disposition 的作用:当Content-Type 的类型为要下载的类型时 , 这个息头会告诉浏览器这个文件的名字和类型。
response.setHeader(“Content-disposition”,“attachment;filename=”+ new String(fileName.getBytes(“UTF-8”),(“iso8859-1”)));
AppAndGroupListJson appAndGroupListJson = appAndGroupListJsonSer