@GetMapping("/downloadByPath")
public void downloadByPath(String path, HttpServletResponse response) {
String localPath = this.applicationProperties.getFile().getLocalDiskPath();//application.yml中配置的路径
FileInputStream fs = null;
OutputStream os = null;
try {
File file = new File(localPath + "/"+ path);
fs = new FileInputStream(file);
String fileName = file.getName();
byte[] buffer = new byte[fs.available()];
fs.read(buffer);
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(),"iso-8859-1"));
response.addHeader("Content-Length", "" + file.length());
os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java下载本地excel文件
最新推荐文章于 2024-08-04 11:07:52 发布