Java Web读取properties配置文件

本文介绍了在Java Web应用中如何读取src目录下的`mailServer.properties`配置文件。通过GetProperty类实现配置文件内容的获取,并在action代码中进行具体使用。

java action读取src目录下的properties配置文件。

mailServer.properties配置文件如下:

mailServerHost = smtp.163.com
mailServerPort = 25
authValidate = true
userName = test@163.com


读取配置文件类GetProperty代码如下:

package com.hsinghsu.test.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class GetProperty {

	// 方法一:通过java.util.ResourceBundle读取资源属性文件
	public static String getPropertyByName(String path, String name) {
		String result = "";

		try {
			// 方法一:通过java.util.ResourceBundle读取资源属性文件
			result = java.util.ResourceBundle.getBundle(path).getString(name);
			System.out.println("name:" + result);
		} catch (Exception e) {
			System.out.println("getPropertyByName2 error:" + name);
		}
		return result;
	}

	// 方法二:通过类加载目录getClassLoader()加载属性文件
	public static String getPropertyByName2(String path, String name) {
		String result = "";

		// 方法二:通过类加载目录getClassLoader()加载属性文件
		InputStream in = GetProperty.class.getClassLoader()
				.getResourceAsStream(path);
		// InputStream in =
		// this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");

		// 注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用
		// InputStream in =
		// Object.class.getResourceAsStream("/mailServer.properties");
		Properties prop = new Properties();
		try {
			prop.load(in);
			result = prop.getProperty(name).trim();
			System.out.println("name:" + result);
		} catch (IOException e) {
			System.out.println("读取配置文件出错");
			e.printStackTrace();
		}
		return result;
	}

}

action代码如下:

调用action,即可获取相应配置文件的属性值。
package com.hsinghsu.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

	private static final long serialVersionUID = 3348881101306356364L;

	public String test(){
		
		System.out.println("=="+GetProperty.getPropertyByName("mailServer","userName"));
		
		System.out.println("==>>"+GetProperty.getPropertyByName2("mailServer.properties","userName"));
		
//		//以下参数从properties文件读取
//				String mailServerHost = null; // 发送邮件的服务器的IP
//				String mailServerPort = null; // 发送邮件的服务器端口
//				String userName = null; // 登陆邮件发送服务器的用户名
//				boolean authValidate = false; // 是否需要身份验证
//				
//				try {
//					//方法一:通过java.util.ResourceBundle读取资源属性文件
//					mailServerHost = java.util.ResourceBundle.getBundle("mailServer").getString("mailServerHost");
//					System.out.println("mailServerHost:"+mailServerHost);
//				} catch (Exception e) {
//					System.out.println("mailServerHost error:"+mailServerHost);
//				}
//				
//				//方法二:通过类加载目录getClassLoader()加载属性文件
////				InputStream in = TestAction.class.getClassLoader().getResourceAsStream("mailServer.properties");
//				InputStream in = this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");
//				
//				//注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用
////				InputStream in = Object.class.getResourceAsStream("/mailServer.properties"); 
//				Properties prop = new Properties();
//				try {
//					prop.load(in);
//					mailServerHost = prop.getProperty("mailServerHost").trim();
//					mailServerPort = prop.getProperty("mailServerPort").trim();
//					userName = prop.getProperty("userName").trim();
//					authValidate = prop.getProperty("authValidate").trim().equalsIgnoreCase("true");
//					
//					System.out.println("mailServerHost:"+mailServerHost+" mailServerPort:"+mailServerPort+" userName:"+userName+" authValidate:"+authValidate);
//				} catch (IOException e) {
//					System.out.println("读取邮箱服务配置文件出错");
//					e.printStackTrace();
//				} 
//        
		return null;
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值