file - 无法访问 bundle 资源/文件 (OSGi)

标签 file resources osgi bundle

目前,我正在使用 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");

当然还有所有可能的路径变体,例如:“configuration/data/config.csv”、“/configuration/data/config.csv”、“\configuration/data/config.csv”、“/config.csv”。
此外,我已将文件夹添加到 OSGi 类路径(在 MANIFEST.MF 中):
Bundle-ClassPath: .,
 configuration/data/

结果 URL 看起来总是像这样(或 null):“configuration/CBR-Data/config.csv”,当我将它传输到文件对象“D:\configuration\CBR-Data\config.csv”时。

但我真正不明白的是,我的 DS 之一的属性文件已完美加载:<properties entry="configuration/dsconfig.properties"/>
有人有想法/提示或其他什么吗?我快疯了...

最佳答案

您正在正确地从包中检索资源。我建议您熟悉 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();
            }
        }
       
  • 转换 URLFile .不推荐这种方法,因为它假设 Bundle 部署为目录(而不是存档)。但是,如果您必须处理需要使用 File 的遗留库,这会很有帮助。对象。要转换为文件,您不能使用 URL.getPath()方法,因为 Equinox 有自己的 URL 格式。您应该使用 org.eclipse.core.runtime.FileLocator类解析为 File . FileLocator.getBundleFile(Bundle)做你想要的。
  • 关于file - 无法访问 bundle 资源/文件 (OSGi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244993/

    相关文章:

    PHP文件上传

    python 将列表写入文件

    scala - 将全局资源文件夹添加到gradle构建中以供子项目使用

    java - 使用 maven 程序集插件覆盖资源文件

    java - 嵌入式 Felix 无法将激活器转换为 BundleActivator

    c# - 将 Web api blob 读入字符串,我可以将其作为 json 对象的一部分发送到服务器并返回到文件

    c++ - 有没有办法从一个目录中读取两条路径?

    Qt:如何关闭配置文件中的资源压缩?

    java - 如何创建支持 Spring 的 Eclipse 插件?

    java - OSGi 中的速度 : how to load templates from classpath