如何处理URL协议包://

时间:2013-03-20 14:29:57

标签: url osgi knopflerfish

我尝试在我的包中找到一个文件:

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

结果是IllegalArgumentException,原因是“URI scheme is not”file“。

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

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

修改

我的解决方案:

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

1 个答案:

答案 0 :(得分:1)

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