Zit*_*zit 11 resources osgi bundle file
目前我正在使用Jetty和Equinox开发基于OSGi的WebApp(参见:http://wiki.eclipse.org/Jetty/Tutorial/EclipseRT-Jetty-Starter-Kit).到目前为止一切都很好,但我无法访问我自己的一些文件/资源.位置/路径是"configuration/data/config.csv"和"configuration/data/data.zip".我测试了一切:
context.getBundleContext().getBundle().getEntry("config.csv");
context.getBundleContext().getBundle().getResource("config.csv");
this.getClass().getClassLoader().getResource("config.csv");
context.getBundleContext().getDataFile("config.csv");
Run Code Online (Sandbox Code Playgroud)
当然所有可能的路径变体如:"configuration/data/config.csv","/ configuration/data/config.csv","\ configuration/data/config.csv","/ config.csv".此外,我已将文件夹添加到OSGi类路径(在MANIFEST.MF中):
Bundle-ClassPath: .,
configuration/data/
Run Code Online (Sandbox Code Playgroud)
生成的URL看起来总是像这样(或null):"configuration/CBR-Data/config.csv",当我将它传输到File对象"D:\ configuration\CBR-Data\config.csv"时.
但我真正不明白的是我的一个DS的属性文件被完美加载:
<properties entry="configuration/dsconfig.properties"/>
有人有想法/提示或其他什么?我开车疯了......
Dan*_*hev 13
您正在从捆绑中正确检索资源.我建议熟悉getEntry(),getResource()和getDataFile()之间的区别.
因为方法会返回正确的URL,这意味着资源位置正确,问题在于您如何阅读它们.
使用它们的两种方法是:
InputStream从URL直接:
URL configURL = context.getBundleContext().getBundle().getEntry("configuration/data/config.csv");
if (configURL != null) {
InputStream input = configUrl.openStream();
try {
// process your input here or in separate method
} finally {
input.close();
}
}
Run Code Online (Sandbox Code Playgroud)
URL为File.建议不要使用此方法,因为它假设Bundle部署为目录(而不是归档).但是,如果必须处理需要使用File对象的旧库,则会很有帮助.要转换为File,您不能使用URL.getPath()方法,因为Equinox有自己的URL格式.您应该使用org.eclipse.core.runtime.FileLocator类来解析为File.这FileLocator.getBundleFile(Bundle)正是你想要的.