2015-11-08 502 views
0

我試圖從kml文件中實時創建kmz文件並將其呈現爲Web應用程序中的字節流。如何從KML使用Java動態創建KMZ文件

但是當我下載生成的kmz文件時,我無法在Ubuntu上使用archive manager打開它。

我在這個網站上看到類似的問題,但它不起作用。

有人可以幫我解釋我做錯了什麼嗎?

這是我的代碼。

@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; 
} 
+0

查看的相關討論,在[此處](http://stackoverflow.com/questions/12749236/unable-to-create-kmz-file-using-java-util-zip),其包括Java片段創建KMZ文件。 – JasonM1

回答

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