动态生成zip文件,并下载

xiaoxiao2021-02-28  4

Maven 依赖设置

<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency>

/** * Download user translated csv files from OpenL10n server to Jenkins server * * @param userEmail * @param productName * @param version * @param languageID * @param response * @return */ @Secured({RoleConstants.IDM_ADMIN, RoleConstants.ROLE_COMMUNITY_ADMIN}) @RequestMapping(value = "/jenkins/downloadSourceCsvZipFile", method = RequestMethod.GET) @ResponseBody public JenkinsResponseDTO downloadSourceCsvZipFile(String userEmail, String productName, String version, String languageID, HttpServletResponse response) { JenkinsResponseDTO jenkinsResponseDTO = new JenkinsResponseDTO(); List<SourceCsvInfo> sourceCsvInfoList = new ArrayList<SourceCsvInfo>(); InputStream in = null; ZipOutputStream zipOutputStream = null; try { LpStatus lpStatus = jenkinsService.getLpStatus(productName, version, userEmail, languageID); List<Lpu> lpuList = jenkinsService.getLpuList(lpStatus.getProjectId()); for (Lpu lpu: lpuList) { List<SourceTargetDTO> sourceTargetDTOs = jenkinsService.getSourceString(lpStatus.getProjectId(), lpu.getLpuID(), lpStatus.getLanguagePackID()); SourceCsvInfo sourceCsvInfo = jenkinsService.generateCsvFile(userEmail, productName, version, lpu.getLpuBaseName(), languageID, sourceTargetDTOs); sourceCsvInfoList.add(sourceCsvInfo); } String downloadFileName = productName + "_" + version + "_" + languageID + ".zip"; response.setStatus(HttpServletResponse.SC_OK); response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadFileName + "\""); zipOutputStream = new ZipOutputStream(response.getOutputStream()); for (int i = 0; i < sourceCsvInfoList.size(); i++) { SourceCsvInfo sourceCsvInfo = sourceCsvInfoList.get(i); in = new FileInputStream(sourceCsvInfo.getPath()); zipOutputStream.putNextEntry(new ZipEntry(sourceCsvInfo.getFileName())); IOUtils.copy(in, zipOutputStream); zipOutputStream.closeEntry();             in.close();          } jenkinsResponseDTO.setResult("Succeed"); } catch (Exception e) { log.debug("downloadSourceCsvZipFile() throw exception is ", e); jenkinsResponseDTO.setResult("Failed"); if (e.getCause() == null) { jenkinsResponseDTO.setMessage(e.getMessage()); } else { jenkinsResponseDTO.setMessage(e.getCause().getMessage()); } } finally { try { if (in != null) { in.close(); } if (zipOutputStream != null) { zipOutputStream.close(); } } catch (Exception e) { if (e.getCause() == null) { jenkinsResponseDTO.setMessage(e.getMessage()); } else { jenkinsResponseDTO.setMessage(e.getCause().getMessage()); } } return jenkinsResponseDTO; } }
转载请注明原文地址: https://www.6miu.com/read-2150169.html

最新回复(0)