package com.ad.web.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
@SuppressWarnings("serial")
public class PropertiesUtil extends HttpServlet {
private static final Logger log = Logger.getLogger(PropertiesUtil.class);
private static Map<String, String> propertiesMap = new HashMap<String, String>();;
public void init(ServletConfig config) throws ServletException {
String localpath=config.getInitParameter("localpath");
String prefix = config.getServletContext().getRealPath("/");
//String file = prefix+"local.properties";
String filePath = prefix + localpath;
Properties props = new Properties();
try {
FileInputStream istream = new FileInputStream(filePath);
props.load(istream);
istream.close();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertiesMap.put(keyStr, value);
}
String path=propertiesMap.get("configdir").toString();
LoadAll(path);
} catch (IOException e) {
return;
}
}
public void LoadAll(String path) {
Properties props = new Properties();
try {
File file = new File(path);//��ȡ�ļ���ַ
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
FileInputStream istream = new FileInputStream(path+"/"+files[i].getName());
props.load(istream);
}
}
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertiesMap.put(keyStr, value);
}
} catch (Exception e) {
log.error("load propertiesMap failure",e);
} finally {
props.clone();
}
}
public static String getProperty(String key)
{
return propertiesMap.get(key);
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
@SuppressWarnings("serial")
public class PropertiesUtil extends HttpServlet {
private static final Logger log = Logger.getLogger(PropertiesUtil.class);
private static Map<String, String> propertiesMap = new HashMap<String, String>();;
public void init(ServletConfig config) throws ServletException {
String localpath=config.getInitParameter("localpath");
String prefix = config.getServletContext().getRealPath("/");
//String file = prefix+"local.properties";
String filePath = prefix + localpath;
Properties props = new Properties();
try {
FileInputStream istream = new FileInputStream(filePath);
props.load(istream);
istream.close();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertiesMap.put(keyStr, value);
}
String path=propertiesMap.get("configdir").toString();
LoadAll(path);
} catch (IOException e) {
return;
}
}
public void LoadAll(String path) {
Properties props = new Properties();
try {
File file = new File(path);//��ȡ�ļ���ַ
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
FileInputStream istream = new FileInputStream(path+"/"+files[i].getName());
props.load(istream);
}
}
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertiesMap.put(keyStr, value);
}
} catch (Exception e) {
log.error("load propertiesMap failure",e);
} finally {
props.clone();
}
}
public static String getProperty(String key)
{
return propertiesMap.get(key);
}
}
<!--web.xml 初始化PropertiesUtil方法 -->
<servlet>
<servlet-name>PropertiesUtil-init</servlet-name>
<servlet-class>com.ad.web.common.PropertiesUtil</servlet-class>
<init-param>
<param-name>localpath</param-name>
<param-value>WEB-INF/classes/local.properties</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>