package com.vincent.rsf.common.utils; import com.vincent.rsf.framework.common.SpringUtils; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import org.yaml.snakeyaml.Yaml; import java.io.BufferedInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class ConfigHelper { public static Properties m_props = new Properties(); public static String m_charset = "GBK"; public static Yaml m_yaml = new Yaml(); public static Map m_root = new HashMap<>(); public static Environment environment; static { try { if (ClassLoader.class.getResource("/application.properties") != null) { m_charset = getCharset(ClassLoader.class.getResourceAsStream("/application.properties")); m_props.load(ClassLoader.class.getResourceAsStream("/application.properties")); } else if ( Thread.currentThread().getContextClassLoader().getResource("/application.properties")!=null) { m_charset = getCharset(Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.properties")); m_props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.properties")); } else if (ConfigHelper.class.getResource("/application.properties")!=null){ m_charset=getCharset(ConfigHelper.class.getResourceAsStream("/application.properties")); m_props.load(ConfigHelper.class.getResourceAsStream("/application.properties")); } } catch (Exception ex){ System.err.println("ERR - undefined application.properties"); } try { if (ClassLoader.class.getResource("/application.yml") != null) { InputStream as = ClassLoader.class.getResourceAsStream("/application.yml"); if(as!=null){ m_root = (Map) m_yaml.load(as); } } else if(Thread.currentThread().getContextClassLoader().getResource("/application.yml")!=null) { InputStream as = Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.yml"); if(as!=null){ m_root = (Map) m_yaml.load(as); } } else if(ConfigHelper.class.getResource("/application.yml")!=null){ InputStream as = ConfigHelper.class.getResourceAsStream("/application.yml"); if(as!=null){ m_root = (Map) m_yaml.load(as); } } } catch (Exception e) { e.printStackTrace(); System.err.println("ERR - undefined application.yml"); } } public static void main(String...strings){ System.out.println(getString("redis.session.index")); } public static Boolean contains(String key){ return getString(key)!=null; } public static String getString(String key) { if (environment==null) { ApplicationContext applicationContext = null; try { applicationContext = SpringUtils.getApplicationContext(); } catch (Exception ignore) {} if ( applicationContext != null) { environment = SpringUtils.getApplicationContext().getEnvironment(); } } if (environment!=null) { String value = environment.getProperty(key); if(value!=null){ return value; } } String value = m_props.getProperty(key); try { if (value!=null){ value = new String(value.getBytes(StandardCharsets.ISO_8859_1),m_charset); } else if(m_root!=null) { String[] items = key.split("\\."); Map map = m_root; for(int i=0;i= 0xF0 ) break; if ( 0x80 <= read && read <= 0xBF ) // 单独出现BF以下的,也算是GBK break; if ( 0xC0 <= read && read <= 0xDF ) { read = bis.read(); if ( !(0x80 <= read && read <= 0xBF) ) // 双字节 (0xC0 - 0xDF) (0x80 // - 0xBF),也可能在GB编码内 { break; } } else if (0xE0 <= read) {// 也有可能出错,但是几率较小 read = bis.read(); if ( 0x80 <= read && read <= 0xBF ) { read = bis.read(); if ( 0x80 <= read && read <= 0xBF ) { charset = "UTF-8"; break; } else break; } else break; } } bis.close(); } return charset; } catch ( Exception e ) { e.printStackTrace(); return null; } } }