文件下载
文件下载
public void downloadFile(String fileCd, HttpServletResponse response) {
WrBuMd wrBuMd = wrBuMdRepository.findById(fileCd).orElse(null);
File file = new File(engingFileUrl + wrBuMd.getFilePath());
FileInputStream fileOutputStream = null;
OutputStream os = null;
try {
fileOutputStream = new FileInputStream(file);
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + new String(wrBuMd.getFileNm().getBytes("gb2312"), "ISO8859-1") + wrBuMd.getFileExt());
os = response.getOutputStream();
byte[] buffer = new byte[1024 * 2];
int count = 0;
while ((count = fileOutputStream.read(buffer)) != -1) {
os.write(buffer, 0, count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
作者: JaminYe
版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。