当前位置: 移动技术网 > IT编程>开发语言>Java > JavaWeb 实现多个文件压缩下载功能

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

文件下载时,我们可能需要一次下载多个文件。批量下载文件时,需要将多个文件打包为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"; 
  } 

总结

以上所述是小编给大家介绍的javaweb 实现多个文件压缩下载功能,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网