批量导出下载
查询出需要下载附件的集合,下载附件到临时目录,压缩下载到临时文件夹的附件,生成压缩包,最后下载压缩包
@Override
@ApiOperation(value = "导出Word压缩文件啊", notes = "2022-05-29 创建:<br/>2022-05-** 完成:")
public void batchExport(HttpServletResponse response) throws IOException {
//查询需要导出附件的地址
List<Report> reportList = reportMapper.selectList(lambda);
//附件导出存放地址
string reportPath="c:/user/download";
//创建需要导出的目录
SimpleDateFormat sfm = new SimpleDateFormat("yyyyMMddHHmm");
String DownloadPath = reportPath + "DownLoad" + sfm.format(new Date());
//获取目录是否存在
File filePhat = new File(DownloadPath);
if (!filePhat.exists()) {
filePhat.mkdirs();
}
//循环附件地址
for (Report entity : reportList) {
log.info("原文件地址:" + reportPath + entity.getReportName());
downLoad(reportPath + "文件名.word", DownloadPath + "/" + "文件名.word");
}
// 创建临时文件
File zipFile = null;
FileInputStream fis = null;
BufferedInputStream buff = null;
try {
//压缩包临时文件名称
zipFile = File.createTempFile("test", ".zip");
FileOutputStream fot = new FileOutputStream(zipFile);
// 为任何OutputStream产生校验,第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种)
CheckedOutputStream cos = new CheckedOutputStream(fot, new Adler32());
// 用于将数据压缩成Zip文件格式
ZipOutputStream zos = new ZipOutputStream(cos);
compress(zos, filePhat, "", "");
zos.close();
ServletOutputStream os = response.getOutputStream();
//下载文件,使用spring框架中的FileCopyUtils工具
String date = "文件名" + sfm.format(new Date());
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系, 用UTF-8'zh_cn'来显示声明文件名称中包含中文
String fileName = URLEncoder.encode(date, "UTF-8").replaceAll("\\+", "%20");
//设置响应头,attachment表示以附件的形式下载,inline表示在线打开
response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".zip");
fis = new FileInputStream(zipFile);
buff = new BufferedInputStream(fis);
FileCopyUtils.copy(buff, os);
} catch (Exception e1) {
// 关闭流
} finally {
try {
if (null != fis) {
fis.close();
}
if (null != buff) {
buff.close();
}
} catch (IOException e) {
}
// 删除临时文件
if (null != zipFile) {
zipFile.delete();
}
//删除临时导出文件
deleteFile(filePhat);
}
}
下载文件到临时文件夹
@ApiOperation(value = "下载附件到临时文件夹", notes = "2022-05-29 创建:<br/>2022-05-** 完成:")
public void downLoad(String oldPath, String newPath) {
OutputStream outputStream = null;
try {
//获取模板地址
String templatePath = oldPath;
InputStream inputStream = new FileInputStream(new File(templatePath));
// 输出位置
outputStream = new FileOutputStream(newPath);
int bytesRead = 0;
while ((bytesRead = inputStream.read()) != -1) {
outputStream.write(bytesRead);
//downloadFile.flush();
}
outputStream.close();
inputStream.close();
} catch (Exception e) {
} finally {
}
}
添加临时文件夹附件到压缩包
@ApiOperation(value = "压缩文件", notes = "2022-05-29 创建:<br/>2022-05-** 完成:")
public static void compress(ZipOutputStream out, File sourceFile, String base, String baseUrl) {
FileInputStream fos = null;
BufferedInputStream bis = null;
try {
//如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
if (flist.length == 0) {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
out.putNextEntry(new ZipEntry(base + "/"));
} else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
//如果不是最上级文件夹,则添加当前文件夹目录
if (!StringUtil.isEmpty(base)) {
baseUrl = baseUrl + sourceFile.getName() + "/";
}
for (int i = 0; i < flist.length; i++) {
compress(out, flist[i], flist[i].getName(), baseUrl);
}
}
} else {//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
out.putNextEntry(new ZipEntry(baseUrl + base));
fos = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fos);
int tag;
//将源文件写入到zip文件中
while ((tag = fos.read()) != -1) {
out.write(tag);
}
bis.close();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
删除临时文件夹
@ApiOperation(value = "删除临时文件", notes = "2022-05-29 创建:<br/>2022-05-** 完成:")
public static Boolean deleteFile(File file) {
//判断文件不为null或文件目录存在
if (file == null || !file.exists()) {
System.out.println("文件删除失败,请检查文件是否存在以及文件路径是否正确");
return false;
}
//获取目录下子文件
File[] files = file.listFiles();
//遍历该目录下的文件对象
for (File f : files) {
//判断子目录是否存在子目录,如果是文件则删除
if (f.isDirectory()) {
//递归删除目录下的文件
deleteFile(f);
} else {
//文件删除
f.delete();
//打印文件名
System.out.println("文件名:" + f.getName());
}
}
//文件夹删除
file.delete();
System.out.println("目录名:" + file.getName());
return true;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。