如何使用 Java 从 KML 动态创建 KMZ 文件

How to create KMZ file from KML on the fly using Java

我正在尝试从 kml 文件动态创建 kmz 文件并将其呈现为 Web 应用程序中的字节流。

但是当我下载生成的 kmz 文件时,我无法使用 Ubuntu 上的存档管理器打开它。

我在这个网站上查看了类似的问题,但是没有用。

谁能帮我解释一下我做错了什么?!

这是我的代码。

@Public public void retrieveKmlInOldFormat() {
   File file = new File(Play.applicationPath+"/"+Play.configuration.getProperty("web.content", "../bspb-web")+"/map/map.kml");
   String kmlFileContent = null;
   try {
     String kmlUrl = file.toURI().toURL().toString();
     kmlFileContent = BSPBKml2OldFormatConverter.toOldKml(
                        kmlParserLocal.load(kmlUrl));
   } catch (MalformedURLException e) {
      e.printStackTrace();
   }
   String zippedFileName = "old_fmt_map.kmz";
   String zippedKml = compressKmlFile(kmlFileContent,zippedFileName);
   response.setContentTypeIfNotSet("application/vnd.google-earth.kmz");
   renderBinary(new ByteArrayInputStream(zippedKml.getBytes()),zippedFileName);
   return;
}

压缩方法代码:

private String compressKmlFile(String kmlFileContent,String zipEntryName){

String zippedContent = null;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

ZipOutputStream zipStream = new ZipOutputStream(new
  BufferedOutputStream(byteStream));

ZipEntry zipEntry = null;
zipEntry = new ZipEntry("doc.kml");
try {
  zipEntry.setSize(kmlFileContent.getBytes("UTF-8").length);
  zipStream.putNextEntry(zipEntry);
  zipStream.write(kmlFileContent.getBytes("UTF-8"));
  zipStream.closeEntry();      
  zippedContent = new String(byteStream.toByteArray(),"UTF-8");
} catch (IOException e) {
  logger.error("Error while zipping kml file content");
}
finally {
  try {
    byteStream.close();
    zipStream.close();
  } catch (IOException e) {
    logger.error(e.getMessage());
  }
}
return zippedContent;
}

问题与下载的损坏的 kmz 存档有关。这个问题可以通过使用 http 响应的输出流作为 ZipOutputStream class.

的构造函数参数来解决

解决方案在这段代码中。

@Public public void retrieveKmlInOldFormat(){
 File file = new File(Play.applicationPath+"/"+Play.configuration.getProperty("web.content", "../bspb-web")+"/map/map.kml");
 String kmlFileContent = null;
 try {
   String kmlUrl = file.toURI().toURL().toString();
   kmlFileContent = BSPBKml2OldFormatConverter.toOldKml(kmlParserLocal.load(kmlUrl));
 } catch (MalformedURLException e) {
   e.printStackTrace();
 }
 response.setContentTypeIfNotSet("application/vnd.google-earth.kmz");
 response.setHeader("Content-Disposition", "attachment; filename=\"old_fmt_map.kmz\"");
 renderAsKmz(response, kmlFileContent,"old_fmt_map.kml");
 return;
}



private void renderAsKmz(Response response,String kmlFileContent,String zipEntryName){
 ZipOutputStream zipStream = new ZipOutputStream(response.out);
 ZipEntry zipEntry = new ZipEntry(zipEntryName);
 try {
  zipStream.putNextEntry(zipEntry);
  zipStream.write(kmlFileContent.getBytes());
 } catch (IOException e) {
   logger.error("Error while zipping kml file content : " + e.getMessage());
 }
 finally {
   try {
     zipStream.closeEntry();
     zipStream.close();
   } catch (IOException e) {
    logger.error("Error while closing zipped stream : " + e.getMessage());
  }
}