spring boot 上線會壓縮文件 首先是要在pom中去除該後綴名文件的壓縮,否者壓縮後文件內容會導致亂碼。
@RequestMapping(value = "/downloadFile")
public void downloadFile(HttpServletResponse response,HttpServletRequest request) throws Exception{
//linux線上適用
ClassPathResource resource = new ClassPathResource("distributorDemo.xls");
//windows適用
String filePath = "./system/src/main/resources/distributorDemo.xls";
File f = new File(new String(filePath.getBytes("ISO8859-1"),"utf-8"));
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ (f.getName()));
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
out.flush();
br.close();
out.close();
}
本文爲記錄貼,方便以後用到快速查找使用