我正在创建一个Webstart应用程序,我需要检索放在jar中文件夹内的一些文件。 我需要将包含内容的整个文件夹复制到本地驱动器。我该怎么做? 似乎Webstart只批准输入流来从jar读取。所以文件夹必须以某种方式读取和输入流。
谢谢!
答案 0 :(得分:2)
正如info embedded-resource中所述,您可以使用getResource()
为每个文件获取URL
。这个简单的example缓存getImage()
中的图片;这个更精细的example构建了RCImage
中的图像集。两者都从JAR文件中名为images
的文件夹中读取。
答案 1 :(得分:1)
使用zipentry Zipinputstream和FileOutPutStream解决它。
注意:只有在Eclipse JVM之外运行它时才有效(例如在运行Java Webstart的情况下)。
CodeSource src = MAINCLASS.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
ZipEntry e = null;
while ((e = zip.getNextEntry()) != null) {
System.out.println("Entry name: " + e.getName());
if (e.getName().endsWith(".db")) {
FileOutputStream outPutFile = new FileOutputStream(folderDest + File.separator
+ e.getName().substring(e.getName().indexOf("INSERT-CHAR-WHERE-TO-BEGIN-RETRIVE-FILE -NAME")));
int data = 0;
while ((data = zip.read()) != -1) {
outPutFile.write(data);
}
outPutFile.close();
}
}
zip.close();
} else {
Log.error("Can't retrive running jar.");
}