日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

關于springmvc下服務器文件打包成zip格式下載功能

 青_春 2017-07-11

近期,項目要求把服務器存儲的上傳文件,批量下載到本地.參考網上資料,實現了服務器文件打包成壓縮文件然后down到本地功能. 
具體代碼實現: 
1、在服務器端創(chuàng)建一個臨時zip格式文件 
2、用jdk自帶的API將服務器所有文件輸入到zip文件中 
3、將zip文件下載到本地,并刪除服務器端zip文件

@RequestMapping(value = "/downloadZip.do")
    public String downloadFiles(String tcLwIds, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<File> files = new ArrayList<File>();
        File Allfile = new File(request.getSession().getServletContext().getRealPath("/") + "upload/");
        if (Allfile.exists()) {
            File[] fileArr = Allfile.listFiles();
            for (File file2 : fileArr) {
                files.add(file2);
            }
        }

        String fileName = UUID.randomUUID().toString() + ".zip";
        // 在服務器端創(chuàng)建打包下載的臨時文件
        String outFilePath = request.getSession().getServletContext().getRealPath("/") + "upload/";

        File fileZip = new File(outFilePath + fileName);
        // 文件輸出流
        FileOutputStream outStream = new FileOutputStream(fileZip);
        // 壓縮流
        ZipOutputStream toClient = new ZipOutputStream(outStream);
    //  toClient.setEncoding("gbk");
        zipFile(files, toClient);
        toClient.close();
        outStream.close();
        this.downloadFile(fileZip, response, true);
        return null;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

將服務器文件存到壓縮包中

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
                    // and positions the stream for
                    // writing the next entry
                    bInStream.close(); // 關閉
                    inStream.close();
                }
            } else {
                throw new ServletException("文件不存在!");
            }
        } catch (IOException e) {
            throw e;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

下載文件

 public void downloadFile(File file,HttpServletResponse response,boolean isDelete) {
          try {
              // 以流的形式下載文件。
              BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
              byte[] buffer = new byte[fis.available()];
              fis.read(buffer);
              fis.close();
              // 清空response
              response.reset();
              OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
              response.setContentType("application/octet-stream");
              response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
              toClient.write(buffer);
              toClient.flush();
              toClient.close();
              if(isDelete)
              {
                  file.delete();        //是否將生成的服務器端文件刪除
              }
           } 
           catch (IOException ex) {
              ex.printStackTrace();
          }
      } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

單個文件的下載

@RequestMapping(value="/singleDownload.do")
    public String singleDownload(String fileName, HttpServletRequest request,
            HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String path = realPath+"upload/";
        File file = new File(path+ File.separator + fileName);
        downloadFile(file, response, false);
        return null;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多