OSGI如何读取插件中的资源文件

[size=medium]思路就是通过bundleContext来取得资源。
首先,要在对应的插件中先建立一个Activator需要实现BundleActivator接口,
代码[/size]:

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

private static BundleContext bundleContext;

public static BundleContext getBundleContext() {
return bundleContext;
}

public void start(BundleContext context) throws Exception {
Activator.bundleContext = context;
}

public void stop(BundleContext context) throws Exception {

}
}



[size=medium] 然后再需要查找资源的地方,取得bundleContext,通过bundleContext的getResource方法取得URL类型的resource,代码:[/size]


public static InputStream getResourceByContext(String path) {
try {
BundleContext bundleContext = Activator.getBundleContext();
URL resource = bundleContext.getBundle().getResource("/web" + path);
InputStream in = resource.openStream();
if (in == null) {
String msg = "\nNot found \"" + path + "\";";
log.error(msg);
}
return in;
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}


[size=medium][color=red]注意这里的路径,是从直接写工程文件夹下开始写。[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值