public void downloadFileByFileId(String fileId,HttpServletResponse response)
throws IOException {
FileUploadPO fileUploadPO=fileUploadDao.getById(fileId);
if (null!=fileId && !fileId.equals("")) {
File file = new File(fileUploadPO.getFilePath());
if (file.exists()) { // 文件存在
response.setHeader("Content-Length", String.valueOf(file.length()));
try {
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[inputStream.available()]; // int available() 返回值为流中尚未读取的字节的数量
inputStream.read(buffer);
inputStream.close();
String fileName = fileUploadPO.getFileName();// 获取文件名
response.reset();
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} //end if (file.exists())
} else {
}
}
文件下载
最新推荐文章于 2022-07-25 19:29:01 发布