栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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> 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 callable = () -> {
            countDownLatch.await();
            File file = new File(zipPath);
            ShapeUtil2.createAllZip(zipPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            deleteFiles(zipPathPrefix);
            return fileInputStream;
        };
        FutureTask futureTask = new FutureTask<>(callable);
        new Thread(futureTask).start();
        return futureTask.get();
    }

controller:

 public ResponseEntity 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));
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/865452.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号