我使用的Jboss服务器,在服务器中增加一个配置文件,文件的路径如下
然后定义这样的类
package com.gzydt.license.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @since 2014-9-1
*
*/
public class ConfigUtil {
private static final String configFile = "gzydt/config/licenses.properties";
static {
Properties prop = new Properties();
InputStream in = null;
try {
in = new FileInputStream(new File(configFile));
} catch (FileNotFoundException e1) {
System.out.println("从" + new File(configFile).getAbsolutePath()
+ "加载licenses.properties失败,重新加载默认配置信息。");
in = ConfigUtil.class.getClassLoader().getResourceAsStream(
"licenses.properties");
}
try {
prop.load(in);
getLicensInfo = prop.getProperty("CATALOGUE_GETLICESEINFO").trim();
getAllLicenseInfo = prop.getProperty(
"CATALOGUE_GETLALLICESETEMPLET").trim();
getPictures = prop.getProperty("CATALOGUE_GETPICTURE").trim();
xmlPath = prop.getProperty("XMLPATH").trim();
pdf1 = prop.getProperty("PDF1").trim();
pdf2 = prop.getProperty("PDF2").trim();
pictures = prop.getProperty("PICTURESPATH").trim();
icon = prop.getProperty("PICTUREICONPATH").trim();
pdfCenterPaht = prop.getProperty("CENTERADDRESS").trim();
localpdf = prop.getProperty("LOCALPDF").trim();
ewmInfo= prop.getProperty("EWMINFO").trim();
log_address = prop.getProperty("LOGADDRESS").trim();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {// 一定要关闭输入流
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 从目录中获取证照信息接口的地址
*/
private static String getLicensInfo;
/**
* 从目录中获取所有证照的模板的地址
*/
private static String getAllLicenseInfo;
/**
* 从目录中获取证照的底图
*/
private static String getPictures;
/**
* 手动录入数据或批量导入数据生成xml的目录
*/
private static String xmlPath;
/**
* 生成pdf1的目录
*/
private static String pdf1;
/**
* 生成pdf2的目录
*/
private static String pdf2;
/**
* 存放底图的目录
*/
private static String pictures;
/**
* 存放公章的目录
*/
private static String icon;
/**
* 生成pdf的路径地址
*/
private static String pdfCenterPaht;
/**
* 本地pdf的路径
*
*/
private static String localpdf;
/**
* 扫描二维码的中心端的地址
*
*/
private static String ewmInfo;
/**
* 系统日志文件地址
*/
private static String log_address;
public static void setLocalpdf(String localpdf) {
ConfigUtil.localpdf = localpdf;
}
public static String getLocalpdf() {
return localpdf;
}
public static void setPdfCenterPaht(String pdfCenterPaht) {
ConfigUtil.pdfCenterPaht = pdfCenterPaht;
}
public static String getPdfCenterPaht() {
return pdfCenterPaht;
}
public static String getGetLicensInfo() {
return getLicensInfo;
}
public static void setGetLicensInfo(String getLicensInfo) {
ConfigUtil.getLicensInfo = getLicensInfo;
}
public static String getGetAllLicenseInfo() {
return getAllLicenseInfo;
}
public static void setGetAllLicenseInfo(String getAllLicenseInfo) {
ConfigUtil.getAllLicenseInfo = getAllLicenseInfo;
}
public static String getGetPictures() {
return getPictures;
}
public static void setGetPictures(String getPictures) {
ConfigUtil.getPictures = getPictures;
}
public static String getXmlPath() {
return xmlPath;
}
public static void setXmlPath(String xmlPath) {
ConfigUtil.xmlPath = xmlPath;
}
public static String getPdf1() {
return pdf1;
}
public static void setPdf1(String pdf1) {
ConfigUtil.pdf1 = pdf1;
}
public static String getPdf2() {
return pdf2;
}
public static void setPdf2(String pdf2) {
ConfigUtil.pdf2 = pdf2;
}
public static String getPictures() {
return pictures;
}
public static void setPictures(String pictures) {
ConfigUtil.pictures = pictures;
}
public static String getIcon() {
return icon;
}
public static void setIcon(String icon) {
ConfigUtil.icon = icon;
}
public static String getEwmInfo() {
return ewmInfo;
}
public static void setEwmInfo(String ewmInfo) {
ConfigUtil.ewmInfo = ewmInfo;
}
public static String getLog_address() {
return log_address;
}
public static void setLog_address(String log_address) {
ConfigUtil.log_address = log_address;
}
}
另一种解析配置文件,这种是比较适合于只查询一条数据的时候
package com.gzydt.license.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 调用配置文件里的路径,弃用static
*
* @author ganyangxing
* @since 2014-11-10
*
*/
public class ConfigUrlUtil {
@SuppressWarnings("resource")
public Properties init(){
String configFile = "gzydt/config/licenses.properties";
Properties prop = new Properties();
InputStream in = null;
try {
in = new FileInputStream(new File(configFile));
} catch (FileNotFoundException e1) {
in = ConfigUrlUtil.class.getClassLoader().getResourceAsStream("licenses.properties");
}
try {
prop.load(in);
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( null != in ) {
try {// 一定要关闭输入流
in.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
return prop;
}
//获取日志文件路径
public String getLogAddress(){
String path = null;
try {
Properties prop = init();
path = prop.getProperty("LOGADDRESS").trim();
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
}