public void downloadClueFileDto(String fileIds, HttpServletRequest request, HttpServletResponse response) {
List<String> strings = Arrays.asList(fileIds.split("&"));
List<ClueFileDto> clueFileDtos = clueFileDao.findByIds(strings);
if (CollectionUtils.isEmpty(clueFileDtos)){
throw new BusinessException("数据不存在");
}
//判断如果是单个文件下载
if (clueFileDtos.size() == 1){
for (ClueFileDto clueFileDto : clueFileDtos) {
String msg = "";
if (StringUtils.isBlank(clueFileDto.getFileAddress())) {
log.error("{},该附件不存在",clueFileDto.getId());
msg = "该附件不存在";
responseOutWithJson(response, msg);
return;
}
// 处理文件名
String fileName = clueFileDto.getFileName();
String fileAddress = clueFileDto.getFileAddress();
File file = new File(fileAddress);
// 读取要下载的文件,保存到文件输入流
InputStream in = null;
// 输出流
BufferedOutputStream bos = null;
try {
in = new FileInputStream(fileAddress);
if (in == null) {
log.error("{},附件文件不存在",clueFileDto.getId());
msg = "附件文件不存在";
responseOutWithJson(response, msg);
return;
}
response.setContentType("application/x-msdownload");
response.setContentLength((int) (file.length()));
// 设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
bos = new BufferedOutputStream(response.getOutputStream());
// 创建缓冲区
byte[] buffer = new byte[1024 * 1024];
int len = 0;
// 循环将输入流中的内容读取到缓冲区当中
while ((len = in.read(buffer)) > 0) {
// 输出缓冲区的内容到浏览器,实现文件下载
bos.write(buffer, 0, len);
}
bos.flush();
response.flushBuffer();
} catch (FileNotFoundException e) {
log.error("Download file error.", e);
responseOutWithJson(response, "文件不存在");
} catch (UnsupportedEncodingException e) {
log.error("Download file error.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (IOException e) {
log.error("Download file error.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}finally {
try {
if (bos != null) {
bos.close();
}
if (in != null) {
in.close();
}
} catch (IOException ioe) {
throw new RuntimeException("下载附件错误", ioe);
}
}
}
} else {
List<File> files = new ArrayList<>();
int length = 0;
for (ClueFileDto clueFileDto : clueFileDtos) {
String msg = "";
if (StringUtils.isBlank(clueFileDto.getFileAddress())) {
log.error("{},该附件不存在",clueFileDto.getId());
msg = "该附件不存在";
responseOutWithJson(response, msg);
continue;
}
File file = new File(clueFileDto.getFileAddress());
length = length + (int) file.length();
files.add(file);
}
//生成的ZIP文件名为Demo.zip
String strZipName = UUID.randomUUID() + ".zip";
FileInputStream fis = null;
ZipOutputStream out = null;
try {
response.setContentType("application/x-msdownload");
// 设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(strZipName, "UTF-8"));
// 创建缓冲区
byte[] buffer = new byte[1024 * 1024];
out = new ZipOutputStream(response.getOutputStream());
for (int i = 0; i < files.size(); i++) {
fis = new FileInputStream(files.get(i));
out.putNextEntry(new ZipEntry(files.get(i).getName()));
int len;
//读入需要下载的文件的内容,打包到zip文件
while((len = fis.read(buffer))>0) {
out.write(buffer,0,len);
}
}
out.flush();
response.flushBuffer();
} catch (FileNotFoundException e) {
log.error("Download file error.", e);
responseOutWithJson(response, "文件不存在");
} catch (UnsupportedEncodingException e) {
log.error("Download file error.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (IOException e) {
log.error("Download file error.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
finally {
try {
if (out != null) {
out.closeEntry();
out.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException ioe) {
throw new RuntimeException("下载附件错误", ioe);
}
}
}
}
单文件下载或多文件下载(自)
于 2021-05-17 15:47:56 首次发布