private void downloadTeamFile(HttpServletResponse response, File file,
String fileName) throws Exception {
if (file == null)
return;
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
OutputStream outputStream = response.getOutputStream();
try {
byte[] downContext = new byte[dataInputStream.available()];
dataInputStream.read(downContext);
fileInputStream.close();
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
outputStream.write(downContext);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
throw new ApplicationException(e.getMessage());
} finally {
if (null != fileInputStream) {
fileInputStream.close();
fileInputStream = null;
}
if (null != dataInputStream) {
dataInputStream.close();
dataInputStream = null;
}
if (null != outputStream) {
outputStream.close();
outputStream = null;
}
}
}
下载文件
最新推荐文章于 2023-08-28 10:46:16 发布