zhaowenyu
2025-11-25 aae8b5dc994fffdc211b1f0fc860840cb0d4687a
ard-route/src/main/java/com/ard/route/service/impl/UavRouteServiceImpl.java
@@ -178,54 +178,31 @@
        String kml = XML_HEADER + xStream.toXML(kmlInfo);
        String wpml = XML_HEADER + xStream.toXML(wpmlInfo);
        KmzResultVo result = new KmzResultVo();
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ZipOutputStream zipOutputStream = new ZipOutputStream(baos)) {
            zipOutputStream.setLevel(0);
        // 判断本地生成kmz文件
        if (isLocal) {
            try (FileOutputStream fileOutputStream = new FileOutputStream(localFilePath + fileName + ".kmz");
                 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
                zipOutputStream.setLevel(0); // 0 表示不压缩,存储方式
            // 创建 wpmz 目录中的 template.kml 文件条目
            buildZipFile("wpmz/template.kml", zipOutputStream, kml);
            // 创建 wpmz 目录中的 waylines.wpml 文件条目
            buildZipFile("wpmz/waylines.wpml", zipOutputStream, wpml);
                // 创建KMZ文件内容
                buildZipFile("wpmz/template.kml", zipOutputStream, kml);
                buildZipFile("wpmz/waylines.wpml", zipOutputStream, wpml);
            zipOutputStream.finish();
            zipOutputStream.flush();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            result.setPath(localFilePath + fileName + ".kmz");
            // 创建MultipartFile对象
            MultipartFile multipartFile = new MockMultipartFile(
                    fileName + ".kmz",
                    fileName + ".kmz",
                    "application/vnd.google-earth.kmz",
                    new ByteArrayInputStream(baos.toByteArray())
            );
            String kmzFilePath = ossService.upload(kmzPath,multipartFile);
            result.setPath(kmzFilePath);
            return result;
        }else {
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 ZipOutputStream zipOutputStream = new ZipOutputStream(baos)) {
                zipOutputStream.setLevel(0);
                // 创建 wpmz 目录中的 template.kml 文件条目
                buildZipFile("wpmz/template.kml", zipOutputStream, kml);
                // 创建 wpmz 目录中的 waylines.wpml 文件条目
                buildZipFile("wpmz/waylines.wpml", zipOutputStream, wpml);
                zipOutputStream.finish();
                zipOutputStream.flush();
                // 获取文件大小
                Double fileSize = (double) baos.size(); // 字节数
                // 创建MultipartFile对象
                MultipartFile multipartFile = new MockMultipartFile(
                        fileName + ".kmz",
                        fileName + ".kmz",
                        "application/vnd.google-earth.kmz",
                        new ByteArrayInputStream(baos.toByteArray())
                );
                String kmzFilePath = ossService.upload(kmzPath,multipartFile);
                result.setPath(kmzFilePath);
                return result;
            } catch (Exception e) {
                throw new RuntimeException("KMZ文件生成或上传失败", e);
            }
        } catch (Exception e) {
            throw new RuntimeException("KMZ文件生成或上传失败", e);
        }
    }