欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

JavaWeb 实现多个文件压缩下载功能

时间:2021-05-11 08:52:18|栏目:JAVA代码|点击:

文件下载时,我们可能需要一次下载多个文件。批量下载文件时,需要将多个文件打包为zip,然后再下载。

实现思路有两种:

一是将所有文件先打包压缩为一个文件,然后下载这个压缩包,

二是一边压缩一边下载,将多个文件逐一写入到压缩文件中。我这里实现了边压缩边下载。

下载样式:

点击下载按钮,会弹出下载框:

下载后就有一个包含刚刚选中的两个文件:


代码实现:

FileBean

public class FileBean implements Serializable { 
  private Integer fileId;// 主键 
  private String filePath;// 文件保存路径 
  private String fileName;// 文件保存名称 
  public FileBean() { 
  } 
  public Integer getFileId() { 
    return fileId; 
  } 
  public void setFileId(Integer fileId) { 
    this.fileId = fileId; 
  } 
  public String getFilePath() { 
    return filePath; 
  } 
  public void setFilePath(String filePath) { 
    this.filePath = filePath; 
  } 
  public String getFileName() { 
    return fileName; 
  } 
  public void setFileName(String fileName) { 
    this.fileName = fileName; 
  } 
} 

控制层:

@RequestMapping(value = "/download", method = RequestMethod.GET) 
  public String download(String id, HttpServletRequest request, 
      HttpServletResponse response) throws IOException { 
    String str = ""; 
    if (id != null && id.length() != 0) { 
      int index = id.indexOf("="); 
      str = id.substring(index + 1); 
      String[] ids = str.split(","); 
      ArrayList<FileBean> fileList = new ArrayList<FileBean>(); 
      for (int i = 0; i < ids.length; i++) {// 根据id查找genericFileUpload,得到文件路径以及文件名 
        GenericFileUpload genericFileUpload = new GenericFileUpload(); 
        genericFileUpload = genericFileUploadService.find(Long.parseLong(ids[i])); 
        FileBean file = new FileBean(); 
        file.setFileName(genericFileUpload.getFileName()); 
        file.setFilePath(genericFileUpload.getFilePath()); 
        fileList.add(file); 
      } 
      //设置压缩包的名字 
      //解决不同浏览器压缩包名字含有中文时乱码的问题 
      String zipName = "download.zip"; 
      response.setContentType("APPLICATION/OCTET-STREAM"); 
      response.setHeader("Content-Disposition", "attachment; filename="+ zipName); 
      //设置压缩流:直接写入response,实现边压缩边下载 
      ZipOutputStream zipos =null; 
      try{ 
        zipos=new ZipOutputStream(new BufferedOutputStream(response.getOutputStream())); 
        zipos.setMethod(ZipOutputStream.DEFLATED);//设置压缩方法  
      }catch(Exception e){ 
        e.printStackTrace(); 
      } 
      DataOutputStream os=null; 
      //循环将文件写入压缩流 
      for(int i=0;i<fileList.size();i++){ 
        String filePath=fileList.get(i).getFilePath(); 
        String fileName=fileList.get(i).getFileName(); 
        File file=new File(filePath+"/"+fileName);//要下载文件的路径 
        try{ 
          //添加ZipEntry,并ZipEntry中写入文件流 
          //这里,加上i是防止要下载的文件有重名的导致下载失败 
          zipos.putNextEntry(new ZipEntry(i+fileName)); 
          os=new DataOutputStream(zipos); 
          InputStream is=new FileInputStream(file); 
          byte[] b = new byte[100]; 
          int length = 0; 
          while((length = is.read(b))!= -1){ 
            os.write(b, 0, length); 
          } 
          is.close(); 
          zipos.closeEntry(); 
        }catch(Exception e){ 
          e.printStackTrace(); 
        } 
      } 
       //关闭流 
      try { 
        os.flush(); 
        os.close(); 
        zipos.close(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      }       
    } 
    return "redirect:list.jhtml"; 
  } 

总结

上一篇:自定义Spring Security的身份验证失败处理方法

栏    目:JAVA代码

下一篇:java 生成有序账号的实现方法

本文标题:JavaWeb 实现多个文件压缩下载功能

本文地址:http://www.codeinn.net/misctech/118972.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有