乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      關(guān)于springmvc下服務(wù)器文件打包成zip格式下載功能

       青_春 2017-07-11

      近期,項(xiàng)目要求把服務(wù)器存儲(chǔ)的上傳文件,批量下載到本地.參考網(wǎng)上資料,實(shí)現(xiàn)了服務(wù)器文件打包成壓縮文件然后down到本地功能. 
      具體代碼實(shí)現(xiàn): 
      1、在服務(wù)器端創(chuàng)建一個(gè)臨時(shí)zip格式文件 
      2、用jdk自帶的API將服務(wù)器所有文件輸入到zip文件中 
      3、將zip文件下載到本地,并刪除服務(wù)器端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";
              // 在服務(wù)器端創(chuàng)建打包下載的臨時(shí)文件
              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

      將服務(wù)器文件存到壓縮包中

      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; // 流需要分開的數(shù)量
                          int leaveByte = 0; // 文件剩下的字符數(shù)
                          byte[] inOutbyte; // byte數(shù)組接受文件的數(shù)據(jù)
      
                          streamTotal = bInStream.available(); // 通過(guò)available方法取得流的最大字符數(shù)
                          streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分開的數(shù)量
                          leaveByte = (int) streamTotal % MAX_BYTE; // 分開文件之后,剩余的數(shù)量
      
                          if (streamNum > 0) {
                              for (int j = 0; j < streamNum; ++j) {
                                  inOutbyte = new byte[MAX_BYTE];
                                  // 讀入流,保存在byte數(shù)組
                                  bInStream.read(inOutbyte, 0, MAX_BYTE);
                                  outputstream.write(inOutbyte, 0, MAX_BYTE); // 寫出流
                              }
                          }
                          // 寫出剩下的流數(shù)據(jù)
                          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(); // 關(guān)閉
                          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();        //是否將生成的服務(wù)器端文件刪除
                    }
                 } 
                 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

      單個(gè)文件的下載

      @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

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多