单文件下载或多文件下载(自)

	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);
				}
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

william冠威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值