zj
2025-04-04 8ceb6cd5ba9d7f347f2070a3967f31cc070ef4ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package project.cms;
 
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 读取Properties综合类,默认绑定到classpath下的config.properties文件。
 */
public class PropertiesUtilCms {
    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtilCms.class);
    private static String CONFIG_FILENAME = "config/cms.properties";
    private static Properties prop = null;
    
    public PropertiesUtilCms() {
        if (prop == null) {
            loadProperties();
        }
    };
    
    private synchronized static void loadProperties() {
        byte buff[]=null;
        try {
            //Open the props file
            InputStream is=PropertiesUtilCms.class.getResourceAsStream("/" + CONFIG_FILENAME);
            prop = new Properties();
            //Read in the stored properties
            prop.load(is);
        }
        catch (Exception e) {
            System.err.println("读取配置文件失败!!!");
            prop = null;
            logger.error(e.getMessage(), e);
        }
    }
    
    /**
     * 得到属性值
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        if (prop == null) {
            loadProperties();
        }
        
        String value = prop.getProperty(key);
        if(value ==null){          
            return null;
        }    
        return value.trim();    
    }
    
    /**
     * 得到内容包含汉字的属性值
     * @param key
     * @return
     */
    public static String getGBKProperty(String key) {
        String value = getProperty(key);
        try {
            value = new String(value.getBytes("ISO8859-1"),"GBK");
        } catch (UnsupportedEncodingException e) {         
        }
        
        if(value ==null){          
            return null;
        }
        return value.trim();
    }
    
    /**
     * 得到属性值,
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue) {
        if (prop == null) {
            loadProperties();
        }
        
        String value = prop.getProperty(key, defaultValue);
        if(value ==null){          
            return null;
        }    
        return value.trim();   
    }
    
    /**
     * 得到内容包含汉字的属性值,如果不存在则使用默认值
     * @param key
     * @return
     */
    public static String getGBKProperty(String key, String defaultValue) {
        try {
            defaultValue = new String(defaultValue.getBytes("GBK"), "ISO8859-1");
            String value = getProperty(key, defaultValue);
            value = new String(value.getBytes("ISO8859-1"), "GBK");
 
            if (value == null) {
                return null;
            }
            return value.trim();
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
    
    public static String getUTFProperty(String key, String defaultValue) {
        try {
            defaultValue = new String(defaultValue.getBytes("UTF-8"),
                    "ISO8859-1");
            String value = getProperty(key, defaultValue);
            value = new String(value.getBytes("ISO8859-1"), "UTF-8");
 
            if (value == null) {
                return null;
            }
            return value.trim();
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
    
    public static void main(String[] args) {
        System.out.println(PropertiesUtilCms.getProperty("mail.username"));
    }
}