SpringMVC下压缩文件下载

        最近在做文件下载的功能,由于下载的是图片,内容比较大,所以需要先在服务器端进行压缩,压缩后下载到用户本地。

        文件下载的步骤:

         1)在服务器端建立一个临时压缩文件

         2)找到文件路径,用JDK自带的API进行文件压缩

         3)将zip文件下载,文件流输出

         4)删除服务器端临时文件


  文件下载:

@Controller
@RequestMapping(value = "${/multidownload")
public   class  MultiResouceDownload extends BaseController{
  @RequestMapping(value = "/downloadZip")
  public String downloadFiles(List<File> files,HttpServletRequest request,HttpServletResponse response)
          throws ServletException, IOException {    

      String fileName = UUID.randomUUID().toString() + ".zip";
      // 在服务器端创建打包下载的临时文件
      String outFilePath = request.getSession().getServletContext().getRealPath("/");

      File fileZip = new File(outFilePath + fileName);
      // 文件输出流
      FileOutputStream outStream = new FileOutputStream(fileZip);
      // 压缩流
      ZipOutputStream zipOutStream = new ZipOutputStream(outStream);

      zipFile(files, zipOutStream);
      zipOutStream.close();
      outStream.close();
      this.downloadFile(fileZip, response, true);
      return null;
  }

 循环遍历多个文件方法:

 

  //循环压缩多个文件
  public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException {
      try {
          int size = files.size();
          // 压缩列表中的文件
          for (int i = 0; i < size; i++) {
              File file = (File) files.get(i);
              zipFile(file, outputStream);
          }
      } catch (IOException e) {
          throw e;
      }
  }

文件压缩方法:

 

public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException {
      try {
          if (inputFile.exists()) {
              if (inputFile.isFile()) {
                  FileInputStream inStream = new FileInputStream(inputFile);
                  BufferedInputStream bInStream = new BufferedInputStream(inStream);
                  ZipEntry entry = new ZipEntry(inputFile.getName());
                  outputstream.putNextEntry(entry);
                  final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流为10M
                  long streamTotal = 0; // 接受流的容量
                  int streamNum = 0; // 流需要分开的数量
                  int leaveByte = 0; // 文件剩下的字符数
                  byte[] inOutbyte; // byte数组接受文件的数据
  
                  streamTotal = bInStream.available(); // 通过available方法取得流的最大字符数
                  streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分开的数量
                  leaveByte = (int) streamTotal % MAX_BYTE; // 分开文件之后,剩余的数量
  
                  if (streamNum > 0) {
                      for (int j = 0; j < streamNum; ++j) {
                          inOutbyte = new byte[MAX_BYTE];
                          // 读入流,保存在byte数组
                          bInStream.read(inOutbyte, 0, MAX_BYTE);
                          outputstream.write(inOutbyte, 0, MAX_BYTE); // 写出流
                      }
                  }
                  // 写出剩下的流数据
                  inOutbyte = new byte[leaveByte];
                  bInStream.read(inOutbyte, 0, leaveByte);
                  outputstream.write(inOutbyte);
                  outputstream.closeEntry(); // Closes the current ZIP entry

                  bInStream.close(); // 关闭
                  inStream.close();
              }
          } else {
              throw new ServletException("文件不存在!");
          }
      } catch (IOException e) {
          throw e;
      }
  }

总结:

       上述方法就能实现文件的压缩下载,但仔细分析发现还是少一些东西,进行压缩前没有进行校验,不能验证数据的正确性,还需要进行修改,目前正在探索中。。。。

         

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值