springBoot+layui 压缩包 直接下载–或--直接压缩并下载方法
前端代码
layer.confirm('您确定要下载 ' + data.fileZipName + ' 吗?', {icon: 3, title: '下载提示'}, function () {
index = top.layer.msg('文件加载中,请稍候', {icon: 16, time: false, shade: 0.8});
window.location.href = requestLink + "back/tBatchGroup/download?id=" + data.id;
layer.closeAll('dialog');
top.layer.close(index);
});
控制器代码
@GetMapping("/download")
public void getDownload(Long id, HttpServletRequest request, HttpServletResponse response) {
try {
TBatchGroup entity = this.service.getById(id);
String fileName = entity.getFileZipName();
String path = "D:\\pdf\\" + entity.getFileZipName() + ".zip";
InputStream inStream = new FileInputStream(path);
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".zip");
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@GetMapping("/plDownload")
public void plDownload(Long[] ids, String soaType, HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
String soaType1 = DicParamUtil.getDicCodeByType("SOA_TYPE", soaType);
List<String> list = new ArrayList();
List<String> fileNameList = new ArrayList();
for (int i = 0; i < ids.length; i++) {
Long id = ids[i];
EaAccessory eaAccessory = this.service.selectBusId(id, soaType);
if (eaAccessory != null) {
fileNameList.add(eaAccessory.getFileName());
list.add(eaAccessory.getFileSite()+eaAccessory.getFilePath());
}
}
String[] files = list.toArray(new String[list.size()]);
String[] fileName = fileNameList.toArray(new String[fileNameList.size()]);
String zipPath = zipUtil.downloadAllAttachment(list, soaType1, request, response);
InputStream inStream = new FileInputStream(zipPath);
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=" + soaType1 + ".zip");
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
工具代码
package com.soa.ump.server.message;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class zipUtil {
public static String downloadAllAttachment(List<String > allFilePath, String fileName, HttpServletRequest request, HttpServletResponse response) {
List<File> filesList = new ArrayList<>();
File[] files=new File[allFilePath.size()];
String path;
for (int j = 0; j < allFilePath.size(); j++) {
path=allFilePath.get(j);
File file = new File(path);
files[j]=file;
filesList.add(file);
}
fileName = "D://"+fileName;
return downLoadFiles(filesList,fileName,request,response);
}
public static ArrayList<String> Dir(File dirFile) throws Exception {
ArrayList<String> dirStrArr = new ArrayList<String>();
if (dirFile.exists()) {
File files[] = dirFile.listFiles();
for (File file : files) {
if (dirFile.getPath().endsWith(File.separator)) {
dirStrArr.add(dirFile.getPath() + file.getName());
} else {
dirStrArr.add(dirFile.getPath() + File.separator
+ file.getName());
}
}
}
return dirStrArr;
}
public static String downLoadFiles(List<File> files,String filePath ,HttpServletRequest request, HttpServletResponse response) {
try {
filePath = filePath+".zip";
File file = new File(filePath);
if (!file.exists()){
file.createNewFile();
}else{
file.delete();
}
response.reset();
FileOutputStream fous = new FileOutputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(fous);
zipFiles(files, zipOut);
zipOut.close();
fous.close();
return filePath;
}catch (Exception e) {
e.printStackTrace();
}
return "文件下载出错";
}
public static void zipFiles(List files,ZipOutputStream outputStream) {
int size = files.size();
for(int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
}
public static void zipFile(File inputFile,ZipOutputStream ouputStream) {
try {
if (inputFile.exists()) {
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
bins.close();
IN.close();
} else {
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], ouputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}