【问题标题】:How to create KMZ file from KML on the fly using Java如何使用 Java 即时从 KML 创建 KMZ 文件
【发布时间】:2015-11-08 00:08:34
【问题描述】:

我正在尝试从 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;
}

【问题讨论】:

  • 查看相关讨论here,其中包括Java sn-p 创建KMZ 文件。

标签: java playframework zip kml kmz


【解决方案1】:

问题在于下载的损坏的 kmz 存档。这个问题可以通过使用http响应的输出流作为ZipOutputStream类的构造函数参数来解决。

解决方案在此代码中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多