OSGI如何读取插件中的资源文件
答案:1 悬赏:40 手机版
解决时间 2021-03-24 09:59
- 提问者网友:佞臣
- 2021-03-23 18:03
OSGI如何读取插件中的资源文件
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-03-23 18:17
首先,要在 对应 的插 件中 先建 立一 个 Activator 需 要实 现 BundleActivator 接口, 代码: 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 { } } 然后再需要查找资源的地方,取得bundleContext,通过bundleContext 的getResource 方法取得URL 类型的resource,代码: 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; } 注意这里的路径,是从直接写工程文件夹下开始写。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |