获取properties配置文件
项目结构
配置文件存放在web和sys,web和sys哪边需要就获取哪边的配置
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
- 邮件配置类
- @author submail
*/
public class MailConfig extends AppConfig {
public static String APP_ID = null;
public static String APP_KEY = null;
public static String APP_SIGNTYPE = null;
private static Properties pros = null;
static {
pros = new Properties();
try {
InputStream in = getResourceAsStream("app_config.properties");
pros.load(in);
//如果web和sys里的配置信息相同可以直接放在email里,注释上面两行,用这个就行
//我这是web和sys用的两个配置信息,所以这样写,哪边要用调用哪边的配置
//pros.load(MailConfig.class.getResourceAsStream("app_config1.properties"));
APP_ID=pros.getProperty("mail_appid");
APP_KEY=pros.getProperty("mail_appkey");
APP_SIGNTYPE=pros.getProperty("mail_signtype");
} catch (IOException e) {
e.printStackTrace();
}
}
public static InputStream getResourceAsStream(String resource) {
String stripped = resource.startsWith("/") ? resource.substring(1): resource;
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader != null) {
stream = classLoader.getResourceAsStream(stripped);
}
return stream;
}
}