1. 使用场景

文档管理模块,列表中显示的记录的每日文件上传保存的记录.每条数据中有一个字段存放了文件的存储地址文件服务器上

现在需要对列表数据批量下载,将多个文件一起下载并存放到一起通过zip压缩包的形式下载到浏览器

2. 开发思路

因为有些需要是要按照某种分类保存并下载,因为可能存在多层文件夹,所有不能直接通过后去每个文件的流的形式往压缩包里面放,所以这里采用先下载的方式,将网络文件按照给定的规则创建文件夹并存放在本地临时目录,然后再去读写文件装成压缩流下载.

说明: 根据自己实际业务,由于需要批量下载的文件一起超过300兆,所以采用两拆分服务,先请求下载接口,使用多线程下载到服务器临时目录,然后隔一段时间去请求下载服务

2.1 文件批量下载到临时目录

controller层
@GetMapping("step/imgs/download/{id}")
@ApiOperation(value = "将指定巡检任务下的图片下载到本地")
public ResponseEntity uploadToLocal(@PathVariable("id") Integer id){
//这步根据个人的业务来获取下载信息集合
Map> imgMap = exportService.getImgMap(id);
exportService.imgTolocal(imgMap);
return ResponseEntity.ok("图片资源正在下载中,请5分钟以后再访问 : /step/imgs/download?id= ,进行文件下载");
}


imgMap数据结构说明:

key: 文件夹名称

value: 存放文件数据表

字段如下

@ApiModelProperty(value = "ID, 修改时为必填")
private Integer id;
@ApiModelProperty(value = "巡检任务步骤表id")
private Integer taskPointStepId;
@ApiModelProperty(value = "图片,视频url")
private String imgUrl;
@ApiModelProperty(value = "缩略图url")
private String imgThumbnailUrl;
@ApiModelProperty(value = "类型0:图片,1:视频")
private Integer type;
private String pointName;
private String stepName;


service层

void imgTolocal(Map> res);

复制代码

service实现类

@Override
public void imgTolocal(Map> res){
index = 0;
if(res == null || res.isEmpty()){
throw new BadRequestAlertException("has_not_imgList" ,this.getClass().getSimpleName(),"has_not_imgList");
}
// 获取系统临时存放地址
Path path = Paths.get(System.getProperty("java.io.tmpdir"),this.TaskName);
File file = new File(String.valueOf(path));
if(!file.exists()){
file.mkdirs();
}
try {
log.info("开始下载文件:{}", sdf.format(new Date()));
res.forEach((k,v) -> {
new Thread (() -> {
index++;
log.info("开始执行下载线程{}", index);
File fl = new File(String.valueOf(path) + File.separator + k);
if (!fl.exists()) {
fl.mkdirs();
}
if (!CollectionUtils.isEmpty(v)) {
v.stream().forEach(e -> {
try {
log.info("开始下载图片资源id{}", e.getImgUrl());
download(e.getImgUrl(), e.getStepName() + e.getId(), fl.getPath());
} catch (Exception e1) {
e1.printStackTrace();
}
});
}
}).start();
});
} catch (Exception e) {
e.printStackTrace();
} finally {
log.info("所有文件下载完成" ,sdf.format(new Date()));
}
}
private void download(String urlString, String filename,String savePath) throws Exception{
URL url = new URL(urlString);
URLConnection con = url.openConnection();
//设置请求超时为5s
con.setConnectTimeout(5*1000);
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf=new File(savePath);
if(!sf.exists()){
sf.mkdirs();
}
String extensionName = urlString.substring(urlString.lastIndexOf("."));
String newFileName = filename + extensionName;
OutputStream os = new FileOutputStream(sf.getPath()+ File.separator +newFileName);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
}


2.2 从系统临时目录将文件转换流通过浏览器下载(创建定时任务自动删除临时文件)

controller 层

@GetMapping("/step/imgs/download")
@ApiOperation(value = "将指定巡检任务下的资源打成压缩包在浏览器下载")
public void uploadStepImg (
@ApiParam(value = "巡检任务id" ,required = true) @RequestParam(value = "id") Integer id,
HttpServletResponse response
){
Map> imgMap = exportService.getImgMap(id);
exportService.imgToZip(imgMap ,response);
}

复制代码

service 层

void imgToZip(Map> res , HttpServletResponse response);

复制代码

3.service实现

@Override
public void imgToZip(Map> res , HttpServletResponse response){
// 获取系统临时存放地址
if(res == null || res.isEmpty()){
throw new BadRequestAlertException("has_not_imgList" ,this.getClass().getSimpleName(),"has_not_imgList");
}
Path path = Paths.get(System.getProperty("java.io.tmpdir"),this.TaskName);
File file = new File(String.valueOf(path));
if(!file.exists()){
throw new BadRequestAlertException("没有找到相关文件",this.getClass().getSimpleName(),"没有找到相关文件");
}
chenkFile(file ,String.valueOf(path));
zip(String.valueOf(path) ,response);
}
public void chenkFile(File file,String path){
try {
if (file.exists()){
//如果文件不存在
if (!file.isDirectory()){
file.createNewFile();
}
}else {
//如果目录不存在
//创建指定目录文件对象
File file1 = new File(path);
file1.mkdirs();//创建目录
file.createNewFile();//创建文件
}
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
public void zip(String sourceFileName, HttpServletResponse response){
ZipOutputStream out = null;
BufferedOutputStream bos = null;
log.info("开始压缩文件:{}", sdf.format(new Date()));
File sourceFile = new File(sourceFileName);
try {
//将zip以流的形式输出到前台
response.setHeader("content-type", "application/octet-stream");
response.setCharacterEncoding("utf-8");
// 设置浏览器响应头对应的Content-disposition
//参数中 testZip 为压缩包文件名,尾部的.zip 为文件后缀
response.setHeader("Content-disposition",
"attachment;filename=" + new String(this.TaskName.getBytes("gbk"), "iso8859-1")+".zip");
//创建zip输出流
out = new ZipOutputStream(response.getOutputStream());
//创建缓冲输出流
bos = new BufferedOutputStream(out);
//调用压缩函数
compress(out, bos, sourceFile, sourceFile.getName());
out.flush();
log.info("压缩完成:{}" ,sdf.format(new Date()));
} catch (Exception e) {
log.error("ZIP压缩异常:"+e.getMessage(),e);
} finally {
try {
if(bos != null) {
bos.close();
}
if( out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//延迟30分钟执行删除任务 不适用Timer Timer任务执行完成之后不会主动关闭线程而是等待gc回收
new Thread( () -> {
try {
Thread.sleep(1000*60*30);
deleteFile(sourceFile);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
public static void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base){
FileInputStream fos = null;
BufferedInputStream bis = null;
try {
//如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
if (flist.length == 0) {
out.putNextEntry(new ZipEntry(base + File.separator));
} else {
//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
for (int i = 0; i < flist.length; i++) {
compress(out, bos, flist[i], base + File.separator + flist[i].getName());
}
}
} else {
//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
out.putNextEntry(new ZipEntry(base));
fos = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fos);
byte [] buffer = new byte[1024];
int tag;
//将源文件写入到zip文件中
while ((tag = bis.read(buffer)) != -1) {
out.write(buffer, 0 ,tag);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
private void deleteFile(File file){
if (file.exists()) {
if (file.isFile()) {
file.delete();
log.info("正在删除文件{}" ,file.getName());
} else if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
this.deleteFile(files[i]);
}
file.delete();
log.info("正在删除文件{}" ,file.getName());
}
} else {
System.out.println("所删除的文件不存在");
}
}