动态生成zip文件,并下载

CSV文件下载与压缩
本文介绍了一个使用Java实现的功能,该功能从OpenL10n服务器下载用户翻译的CSV文件,并将其压缩为ZIP格式文件供Jenkins服务器使用。具体包括通过HTTP GET请求处理下载过程,利用Maven依赖进行文件操作,以及异常处理。

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;
    }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值