@GetMapping(value ="export")
private void export(HttpServletResponse response,HttpServletRequest request){
ServletOutputStream out = null;
FileInputStream ips = null;
String url = path // 文件路径
try {
log.error(url);
//获取文件存放的路径
File file = new File(url);
if (!file.exists()) {
//如果文件不存在就跳出
log.error("导出文件不存在");
throw new RuntimeException("导出失败!");
}
String fileName = file.getName();
log.info("{}",fileName);
ips = new FileInputStream(file);
response.setContentType("multipart/form-data");
//为文件重新设置名字,采用数据库内存储的文件名称
response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + "\"");
out = response.getOutputStream();
//读取文件流
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = ips.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException("导出失败!");
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error("关闭流出现异常-{}", e);
}
}
if (ips != null) {
try {
ips.close();
} catch (IOException e) {
log.error("关闭流出现异常-{}", e);
}
}
}
}