下载成zip压缩包

    技术2022-05-11  9

    private static final String EXCEL_TEMPLATE_PATH = "/WEB-INF/SeikyuuTemplate.xls";

    private byte[] templateByteArray;

     public void downloadZip(HttpServletResponse response,   HttpServletRequest request, String seikyuu_ym) {

      List seikyuuymList = this.getSeikyuuymTorihikiList(seikyuu_ym);

      String dateOut = getDate();  String zipName = "請求書一括" + dateOut + ".zip";

      response.reset();

      try {   String filePath = getTemplateFilePath(request);   byte[] cachedByteArray = getTemplateByteArray(filePath);

       zipName = new String(zipName.getBytes("MS932"), "8859_1");   response.setHeader("Content-Disposition", "attachment; filename="     + zipName);

       ServletOutputStream outputStream = response.getOutputStream();   BufferedOutputStream bros = new BufferedOutputStream(outputStream);   ZipOutputStream objZos = new ZipOutputStream(bros);   objZos.setEncoding("MS932");

       int i = 0;   for (Iterator iter = seikyuuymList.iterator(); iter.hasNext();) {    i++;

        SeikyuuBean objSeikyuu = (SeikyuuBean) iter.next();    Integer seikyuu_id = objSeikyuu.getSeikyuu_id();    Integer torihiki_nm_id = objSeikyuu.getTorihiki_nm_id();

        // get seikyuu msg    SeikyuuBean seikyuuTorihiki = this      .getSeikyuuTorihikiMsg(seikyuu_id);

        // get seikyuu detail list    List seikyuuDetaillist = this.getSeikyuuCustomList(      torihiki_nm_id, seikyuu_ym);

        ByteArrayOutputStream objBos = new ByteArrayOutputStream();

        ByteArrayInputStream templateIn = new ByteArrayInputStream(      cachedByteArray);

        HSSFWorkbook workbook = new HSSFWorkbook(templateIn);

        this.getExcelGenerator().excelGenerator(workbook,      seikyuuDetaillist, seikyuuTorihiki);

        workbook.write(objBos);

        String xlsName = seikyuuTorihiki.getTorihiki_nm_kanji() + "_"      + dateOut + "_" + i + ".xls";    xlsName = getResetFileName(xlsName);    ZipEntry objZe = new ZipEntry(xlsName);

        objZe.setMethod(ZipOutputStream.DEFLATED);    objZos.putNextEntry(objZe);

        byte[] aryByt = objBos.toByteArray();

        objZos.write(aryByt, 0, aryByt.length);

        objBos.close();   }

       objZos.closeEntry();   objZos.close();   bros.close();   outputStream.close();  } catch (IOException ioe) {   log.error("ExcelFile書き出し中にIOExeptionが発生しました。", ioe);   throw new RuntimeException("失敗しました。", ioe);  } }

     String getTemplateFilePath(HttpServletRequest request) {  return request.getSession().getServletContext().getRealPath(    EXCEL_TEMPLATE_PATH); }

     synchronized byte[] getTemplateByteArray(String filePath) {  if (templateByteArray == null) {   FileInputStream fin = null;   BufferedInputStream bin = null;   try {    fin = new FileInputStream(filePath);    bin = new BufferedInputStream(fin);    ByteArrayOutputStream baos = new ByteArrayOutputStream();    int b = -1;    while ((b = bin.read()) > -1) {     baos.write(b);    }    baos.close();    templateByteArray = baos.toByteArray();    log.debug("templateByteArray.length = "      + templateByteArray.length);   } catch (FileNotFoundException e) {    throw new RuntimeException("TemplateFileが存在しません。"      + e.getMessage(), e);   } catch (IOException e) {    throw new RuntimeException("TemplateFileの読み込みに失敗しました。"      + e.getMessage(), e);   } finally {    if (bin != null) {     try {      bin.close();     } catch (IOException e) {      throw new RuntimeException(        "TemplateFileの読み込み時にStreamのCloseに失敗しました。"          + e.getMessage(), e);     }    }   }  }  return templateByteArray; }

     


    最新回复(0)