/** * 读取数据库配置私有参数值 * * @return */ public static Properties getDBFacelibPropConfig() { if (null == dbFaceCloudConfig) { try { dbFaceCloudConfig = new Properties(); URL url = FaceCloudConfig.class.getClassLoader().getResource(FaceCloudConstant.DB_FACECLOUD_CONFIG_NAME); LOG.info(url.toString()); String facelibConfigPath = FaceCloudConfig.class.getClassLoader() .getResource(FaceCloudConstant.DB_FACECLOUD_CONFIG_NAME).toURI().getPath(); dbFaceCloudConfig = PropertiesUtil.getProperties(facelibConfigPath); // } } catch (Exception e) { LOG.error("*********************************************"); LOG.error("error appear in FacelibConfig getDBFacelibPropConfig method :", e); LOG.error("*********************************************"); } } return dbFaceCloudConfig; }
PropertiesUtil:
/** * @ProjectName: BSP 海康威视大数据服务平台 * @Copyright: 2015 HangZhou Hikvision System Technology Co., Ltd. All Right Reserved. * @address: http://www.hikvision.com * @date: 2015年6月27日 下午2:36:45 * @Description: 本内容仅限于杭州海康威视数字技术股份有限公司内部使用,禁止转发. */ package com.hikvision.bsp.common.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * Properties配置文件工具类 * * @author zhuchao 2015年6月27日 下午2:36:45 * @version V1.0 * @modificationHistory=========================逻辑或功能性重大变更记录 * @modify by user: $author$ $date$ * @modify by reason:{方法名}:{原因} */ public final class PropertiesUtil { private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class); /** * 修改属性文件 * * @author zhuchao 2015年6月27日 下午2:37:14 * @param keys * @param values * @param filePath */ public static void updateProperties(Map<String, String> pros, String filePath) { try { // pros为空则返回 if (pros == null || pros.size() == 0) { return; } if (StringUtil.isBlank(filePath)) { LOG.error("传入配置文件路径不能为空."); return; } File file = new File(filePath); if (!file.exists() || !file.isFile()) { LOG.error("传入配置文件路径:{}不正确", filePath); return; } PropertiesConfiguration pro = new PropertiesConfiguration(file); for (Entry<String, String> entry : pros.entrySet()) { String value = entry.getValue(); if (null == value) { value = ""; } pro.setProperty(entry.getKey(), value); } pro.save(); } catch (ConfigurationException e) { LOG.error("修改配置文件出错", e); } } /** * 读取配置文件 * * @author zhuchao 2015年6月27日 下午2:37:27 * @param file * @return */ public static Properties getProperties(String file) { Properties pro = null; // 从文件mdxbu.properties中读取网元ID和模块ID信息 FileInputStream in = null; try { in = new FileInputStream(new File(file)); pro = new Properties(); pro.load(in); } catch (Exception e) { LOG.error("Read " + file + " IOException:", e); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { LOG.error("Read " + file + " IOException:", e); } } return pro; } }