//后台代码
@RequestMapping(value = "/downloadInvoices", method = RequestMethod.GET)
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) // 数据的回滚,需要测试
public void downloadInvoices(HttpServletResponse response, String ids)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
//读取yml文件
String address = env.getProperty("file.download.url"); //如果想指定下载位置
String uploadUrl = env.getProperty("file.url"); //上传时配置的路径
try {
List<String> paths = new ArrayList<>();
if (ids.length()>0 && !"".equals(ids) && ids!=null){
String[] splits = ids.split(",");
for (String id : splits){
InvoiceFile invoiceFile = invoiceFileService.get(id);
String folder = invoiceFile.getFileId(); //文件夹的名称
String fileName = invoiceFile.getFileNum(); //文件名称
String filePath = uploadUrl + "/" + folder + "/" + fileName +".pdf";
System.out.println(filePath);
paths.add(filePath);
}
}
String date = DateUtil.format(new Date(),"yyyyMMddHHmmss");
if (paths.size() != 0) {
File file = new File(address);
if (!file.isDirectory()){
file.mkdir();
}
// String zipFilePath = address+"/批量下载"+date+".zip"; //本地生效
String zipFilePath = "D:\\invoiceupload\\zip\\test.zip"; //部署宝塔上生效
// 压缩输出流,包装流,将临时文件输出流包装成压缩流,将所有文件输出到这里,打成zip包
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
for (String path:paths){
fileToZip(path,zipOut); //这个抽取成方法,在下面
}
zipOut.close(); //关流
//拼接下载默认名称并转为ISO-8859-1格式
String fileName = new String(("批量下载"+date+".zip").getBytes(),"ISO-8859-1");
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attchment;filename="+fileName);
//该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(zipFilePath);
// copy方法为文件复制,在这里直接实现了下载效果
//引入这个包下面的import org.apache.tomcat.util.http.fileupload.IOUtils;
IOUtils.copy(inputStream, outputStream);
inputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
public static void fileToZip(String filePath,ZipOutputStream zipOut) throws IOException { // 需要压缩的文件 File file = new File(filePath); // 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName String fileName = file.getName(); FileInputStream fileInput = new FileInputStream(filePath); // 缓冲 byte[] bufferArea = new byte[1024 * 10]; BufferedInputStream bufferStream = new BufferedInputStream(fileInput, 1024 * 10); // 将当前文件作为一个zip实体写入压缩流,fileName代表压缩文件中的文件名称 zipOut.putNextEntry(new ZipEntry(fileName)); int length = 0; // 最常规IO操作,不必紧张 while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) { zipOut.write(bufferArea, 0, length); } //关闭流 fileInput.close(); // 需要注意的是缓冲流必须要关闭流,否则输出无效 bufferStream.close(); // 压缩流不必关闭,使用完后再关 }
// 前台
// 批量下载 function downloadInvoice() { var checkStatus = table.checkStatus('invoice'), data = checkStatus.data, ids=""; if(data.length > 0) { for (var i in data) { if(ids.length>0){ ids=ids+"," } ids=ids+data[i].id; } layer.confirm('确定批量下载选中的发票?', {icon: 3, title: '提示信息'}, function (index) { //这种直接在浏览器底部出现 location.href = invoiceDownloadUrl+"?ids="+ids; layer.close(index); }) }else{ layer.msg("请选择需要下载的发票",{ time: 2000 },function () {}); }