首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

OSGI怎么读取插件中的资源文件

2012-07-30 
OSGI如何读取插件中的资源文件思路就是通过bundleContext来取得资源。首先,要在对应的插件中先建立一个Acti

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

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

      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;??????}??



    注意这里的路径,是从直接写工程文件夹下开始写。


    另外,楼下说的
    Thread.currentThread().getContextClassLoader().getResource()
    也是可以取到的

热点排行