Java 多文件异步下载,线程通信,最后给主线程输出

service:    
public InputStream downShp(String areaTypeId) throws Exception {
        if (StringUtils.isEmpty(areaTypeId)) {
            areaTypeId = "1";
        }
        int count = 5;
        CountDownLatch countDownLatch = new CountDownLatch(count);
        String path = System.getProperty("user.dir");
        String zipPathPrefix = path + File.separator + "static" + File.separator + "downloadZip" + File.separator + UUIDUtil.createUUID()+File.separator;
        String zipPath =zipPathPrefix+"area.zip";
        File file1 = new File(zipPathPrefix);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        String[] strings = new String[]{"area_sheng", "area_shi", "area_xian", "area_xiang", "area_zerenqu"};
        for (String string : strings) {
            String finalAreaTypeId = areaTypeId;
            taskExecutor.execute(() -> {
                String sql;
                if ("area_sheng".equals(string)) {
                    sql = "select * ,st_astext(geom) as the_geom from " + string + " " ;
                } else {
                    sql = "select * ,st_astext(geom) as the_geom from " + string + " where area_type_id ='" + finalAreaTypeId +"'";
                }
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
                if (mapList != null && mapList.size() > 0) {
                    String prefix = path + File.separator + "static" + File.separator + "download" + File.separator + UUIDUtil.createUUID();
                    File f = new File(prefix);
                    if (!f.exists()) {
                        f.mkdirs();
                    }
                    String shapePath = prefix + File.separator + string + ".shp";
                    ShapeUtil2.write2Shape(shapePath, "MultiPolygon", mapList);
                    log.info("shp文件已生成!路径:" + shapePath);
                    String localPath = prefix + File.separator + string  + ".zip";
                    ShapeUtil2.createZip(localPath);
                    log.info("压缩文件已生成!路径:" + localPath);
                    try {
                        CopyFileUtil.copyFile(new File(localPath),new File(zipPathPrefix + string + ".zip"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //删除本地生成的shp文件
//                    deleteFiles(prefix);
//                    f.delete();
                }
                countDownLatch.countDown();
            });
        }
        Callable<FileInputStream> callable = () -> {
            countDownLatch.await();
            File file = new File(zipPath);
            ShapeUtil2.createAllZip(zipPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            deleteFiles(zipPathPrefix);
            return fileInputStream;
        };
        FutureTask<FileInputStream> futureTask = new FutureTask<>(callable);
        new Thread(futureTask).start();
        return futureTask.get();
    }

controller:

 public ResponseEntity<InputStreamResource> exportXls(@RequestParam(value = "areaTypeId", required = false) String areaTypeId) {
        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        try {
            headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode("区域模板.zip", "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Last-Modified", new Date().toString());
        headers.add("ETag", String.valueOf(System.currentTimeMillis()));

        // 获取文件流
//        InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), "area/template/区域模板.zip");
        InputStream inputStream = null;
        try {
            inputStream = areaManageUpdateLogService.downShp(areaTypeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new InputStreamResource(inputStream));
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值