资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在JavaWeb项目中实现文件压缩下载功能

本篇文章为大家展示了如何在JavaWeb项目中实现文件压缩下载功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目网站制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元成华做网站,已为上家服务,为成华各地企业和个人服务,联系电话:028-86922220

具体代码如下所示:

//文件名称
    String[] names={"one.jpg","two.jpg","three.jpg","four.jpg"};
    //四个文件流
    FileInputStream input1 = new FileInputStream(new File("文件路径"));
    FileInputStream input2 = new FileInputStream(new File("文件路径"));
    FileInputStream input3 = new FileInputStream(new File("文件路径"));
    FileInputStream input4 = new FileInputStream(new File("文件路径"));
    FileInputStream[] inputs={input1,input2,input3,input4};
    //ZIP打包图片
    File zipFile = new File("压缩文件存放路径");
    byte[] buf = new byte[1024];
    int len;
    ZipOutputStream zout=new ZipOutputStream(new FileOutputStream(zipFile));
    for (int i = 0; i < inputs.length; i++) { 
      FileInputStream in =inputs[i]; 
      zout.putNextEntry(new ZipEntry(names[i]));  
      while ((len = in.read(buf)) > 0) { 
        zout.write(buf, 0, len); 
      } 
      zout.closeEntry(); 
      in.close(); 
    }
    zout.close();
    //下载图片
    FileInputStream zipInput =new FileInputStream(zipFile);
    OutputStream out = response.getOutputStream();
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=images.zip");
    while ((len=zipInput.read(buf))!= -1){ 
      out.write(buf,0,len); 
    }
    zipInput.close();
    out.flush();
    out.close();
    //删除压缩包
    zipFile.delete();

上述内容就是如何在JavaWeb项目中实现文件压缩下载功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站名称:如何在JavaWeb项目中实现文件压缩下载功能
文章位置:http://cdkjz.cn/article/gooohi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220