java多文件打包下载成zip

public void downloadProfessionalServiceOrder(DownloadOrderParam param, HttpServletResponse response) throws Exception {
        Order order = orderRepository.findOne(param.getOrderId());
        if (order.getIsDownload()) {
            LocalDateTime completeTime = order.getCompleteTime();
            LocalDateTime now = LocalDateTime.now();
            Duration duration = Duration.between(completeTime, now);
            long days = duration.toDays(); //相差的天数
            if (days > 3) {
                throw new BusinessException("订单下载失败,请核实订单是否已过期");
            }
        }
        order.setIsDownload(true);
        orderRepository.update2(order);
        QueryProfessionalServiceResultParam queryProfessionalServiceResultParam=JSONObject.parseObject(order.getParamContent(),QueryProfessionalServiceResultParam.class);
//        QueryProfessionalServiceResultParam queryProfessionalServiceResultParam = new QueryProfessionalServiceResultParam();
//        queryProfessionalServiceResultParam.setInterfaceId("getMaskProductForeTable");
//        queryProfessionalServiceResultParam.setDate("2021-05-10,2021-05-11");
        List<ProfessionalServiceResultDTO> professionalServiceResultDTOS = professionalServicel.queryProfessionalServiceResult(queryProfessionalServiceResultParam);
        List<File> files = new ArrayList<>();
        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
        BufferedWriter bw = null;
        for (ProfessionalServiceResultDTO professionalServiceResultDTO : professionalServiceResultDTOS) {
            String filePath = orderAddress;
            File dir = new File(filePath);
            if (!dir.exists()) {
                boolean result = dir.mkdirs();
                log.info("新建临时目录结果:" + result);
            }
            if (!professionalServiceResultDTO.getIsUrl()) {
                //data情况下
                String fileName = professionalServiceResultDTO.getTitle() + ".txt";
                bw = new BufferedWriter(new FileWriter(filePath + "/" + fileName));
                bw.write(professionalServiceResultDTO.getUrl());
                bw.flush();
                File file = new File(filePath + "/" + fileName);
                files.add(file);
            } else {
                //url情况下
                String url = professionalServiceResultDTO.getUrl();
                String serverApi = isUseReal ? "http://" + url : "http://39.102.55.70:8008" + url.substring(18);
                URL requsetUrl = new URL(serverApi);
                String fileName = professionalServiceResultDTO.getTitle() + ".pdf";
                File file = new File(filePath + "/" + fileName);
                FileUtils.copyURLToFile(requsetUrl, file);
                files.add(file);
            }
        }
        FileUtil.zipFile(files, "专业服务下载", zos);
        zos.flush();
        zos.close();
        if (bw != null) {
            bw.close();
        }
    }
@UtilityClass
@Slf4j
public class FileUtil {

    private final DataFormatter dataFormatter = new DataFormatter();

    //@描述:是否是2007的excel,返回true是2007
    public static boolean isExcel2007(String fileName) {
        return fileName.matches("^.+\\.(?i)(xlsx)$");
    }

    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell == null) {
            return cellValue;
        }
        //判断数据的类型
        switch (cell.getCellTypeEnum()) {
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case _NONE:
                cellValue = StringUtils.EMPTY;
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellValue = String.valueOf(cell.getDateCellValue().getTime());
                } else {
                    cellValue = dataFormatter.formatCellValue(cell);
                }
                break;
            default:
                cellValue = cell.toString();
        }
        return cellValue;
    }


    public static boolean isRowEmpty(Row row) {
        if (row == null) {
            return true;
        }
        for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c);
            if (StringUtils.isNotBlank(getCellValue(cell))) {
                return false;
            }
        }
        return true;
    }

    public static void zipFile(List<File> subs, String baseName, ZipOutputStream zos) throws IOException {
        for (int i=0;i<subs.size();i++) {
            File f=subs.get(i);
            zos.putNextEntry(new ZipEntry(baseName + f.getName()));
            FileInputStream fis = new FileInputStream(f);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, r);
            }
            fis.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值