java实现多个网络文件批量下载并压缩

编程入门 行业动态 更新时间:2024-10-11 17:27:29

java实现<a href=https://www.elefans.com/category/jswz/34/1771377.html style=多个网络文件批量下载并压缩"/>

java实现多个网络文件批量下载并压缩

1. 使用场景

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

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

2. 开发思路

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

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

2.1 文件批量下载到临时目录
  1. controller层 ```java @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= ,进行文件下载"); }

`` 2.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;

```

  1. service层 ```java void imgTolocal(Map > res);

```

  1. service实现类 ```java @Override public void imgTolocal(Map > res) { index = 0; if(res == null || res.isEmpty()){ throw new BadRequestAlertException("has notimgList" ,this.getClass().getSimpleName(),"has notimgList"); } // 获取系统临时存放地址 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 从系统临时目录将文件转换流通过浏览器下载(创建定时任务自动删除临时文件)
  1. controller 层 ```java @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); }

```

  1. servicejava void imgToZip(Map<String, List<PatrolTaskPointStepImgVO>> res , HttpServletResponse response);

3.service实现 ```java @Override public void imgToZip(Map > res , HttpServletResponse response) { // 获取系统临时存放地址 if(res == null || res.isEmpty()){ throw new BadRequestAlertException("has notimgList" ,this.getClass().getSimpleName(),"has notimgList"); } 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("所删除的文件不存在");}
}

```

更多推荐

java实现多个网络文件批量下载并压缩

本文发布于:2024-03-04 21:39:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1710491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件批量   网络   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!