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