- import org.hibernate.engine.jdbc.StreamUtils;
-
- import java.io.*;
- import java.util.Date;
- import java.util.Properties;
-
- public class PropertiesUtil {
- // 配置文件
- // 配置文件路径
- private static final String confFile = "server/runtime/jdzzReport.properties";
- // 用到的参数配置信息
- private static String API_CODE = null;
- private static String BUSINESS_TYPE = null;
- private static String REPAIRMARK = null;
- private static String USER = null;
- private static String PASSWORD = null;
- private static String reporturl = null;
- private static String DATA_FLAG = null;
- private static String DATA_TYPE = null;
-
-
- // private static String token = "";
- // private static String refreshToken = "";
- // token过期时间,以秒为单位
- // private static Date tokenExpiredTime = new Date();
- // refreshToken过期时间,以秒为单位
- // private static Date refTokenExpiredTime = new Date();
- private static long fileLastModified = 0;
-
- /**
- * 检查文件是否更新过
- *
- * @return
- */
- private static boolean checkFileUpdated() {
- File conf = new File(confFile);
- if (!conf.exists()) {
- throw new RuntimeException("配置文件不存在" + conf.getAbsolutePath());
- }
- long latest = conf.lastModified();
- if (latest > fileLastModified) {
- fileLastModified = latest;
- return true;
- }
- return false;
- }
-
- private static void readProperties() {
- File conf = new File(confFile);
- if (!conf.exists()) {
- throw new RuntimeException("配置文件不存在" + conf.getAbsolutePath());
- }
- Properties properties = new Properties();
-
- try {
- FileInputStream confInput = new FileInputStream(conf);
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- StreamUtils.copy(confInput, byteArrayOutputStream);
- String confText = new String(byteArrayOutputStream.toByteArray(), "utf8");
- properties.load(new StringReader(confText));
- //关闭输入流
- confInput.close();
-
- API_CODE = properties.getProperty("API_CODE");
- BUSINESS_TYPE = properties.getProperty("BUSINESS_TYPE");
- REPAIRMARK = properties.getProperty("REPAIRMARK");
- USER = properties.getProperty("USER");
- PASSWORD = properties.getProperty("PASSWORD");
- reporturl = properties.getProperty("reporturl");
- DATA_FLAG = properties.getProperty("DATA_FLAG");
- DATA_TYPE = properties.getProperty("DATA_TYPE");
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
- //生成的参数get方法
- public static String getApiCode() {
- if (API_CODE == null || API_CODE.equals("") || checkFileUpdated()) readProperties();
- return API_CODE;
- }
-
-
- public static String getBusinessType() {
- if (BUSINESS_TYPE == null || BUSINESS_TYPE.equals("") || checkFileUpdated()) readProperties();
- return BUSINESS_TYPE;
- }
-
-
- public static String getREPAIRMARK() {
- if (REPAIRMARK == null || REPAIRMARK.equals("") || checkFileUpdated()) readProperties();
- return REPAIRMARK;
- }
-
-
- public static String getUSER() {
- if (USER == null || USER.equals("") || checkFileUpdated()) readProperties();
- return USER;
- }
-
-
- public static String getPASSWORD() {
- if (PASSWORD == null || PASSWORD.equals("") || checkFileUpdated()) readProperties();
- return PASSWORD;
- }
-
-
- public static String getReporturl() {
- if (reporturl == null || reporturl.equals("") || checkFileUpdated()) readProperties();
- return reporturl;
- }
-
-
- public static String getDataFlag() {
- if (DATA_FLAG == null || DATA_FLAG.equals("") || checkFileUpdated()) readProperties();
- return DATA_FLAG;
- }
-
- public static String getDataType() {
- if (DATA_TYPE == null || DATA_TYPE.equals("") || checkFileUpdated()) readProperties();
- return DATA_TYPE;
- }
-
- }
使用时直接调用静态的get方法即可,例如:
PropertiesUtil.getUSER()
配置文件中的形式为key=value的形式,例如:USER = zhangsan