【问题标题】:How to handle the URL protocol bundle://如何处理 URL 协议 bundle://
【发布时间】:2013-03-20 14:29:56
【问题描述】:

我尝试在我的包中获取一个文件:

File file = new File(bundleContext.getBundle().
getResource("image/logo.jpg").toURI());

结果是 IllegalArgumentException,原因是“URI 方案不是“文件”。

这是合乎逻辑的,但我应该如何使用这个 URL (bundle://28/image/logo.jpg) 打开一个文件?

如果我使用常规的 ClassLoader,我会得到相同的结果。

编辑:

我的解决方案:

URL url = this.getClass().getClassLoader().getResource("image/logo.jpg");
InputStream in = new BufferedInputstream(url.openStream());

【问题讨论】:

    标签: url osgi knopflerfish


    【解决方案1】:

    您无法打开文件,因为可能没有文件...所以只需获取输入流即可。这适用于所有 URL/URI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-26
      • 2013-09-03
      • 2011-01-15
      • 2013-04-28
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      相关资源
      最近更新 更多