Java Web读取properties配置文件

来源:互联网 发布:中国机床 知乎 编辑:程序博客网 时间:2024/06/05 10:47

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

mailServer.properties配置文件如下:

[plain] view plaincopy
  1. mailServerHost = smtp.163.com  
  2. mailServerPort = 25  
  3. authValidate = true  
  4. userName = test@163.com  


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

[java] view plaincopy
  1. package com.hsinghsu.test.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. public class GetProperty {  
  8.   
  9.     // 方法一:通过java.util.ResourceBundle读取资源属性文件  
  10.     public static String getPropertyByName(String path, String name) {  
  11.         String result = "";  
  12.   
  13.         try {  
  14.             // 方法一:通过java.util.ResourceBundle读取资源属性文件  
  15.             result = java.util.ResourceBundle.getBundle(path).getString(name);  
  16.             System.out.println("name:" + result);  
  17.         } catch (Exception e) {  
  18.             System.out.println("getPropertyByName2 error:" + name);  
  19.         }  
  20.         return result;  
  21.     }  
  22.   
  23.     // 方法二:通过类加载目录getClassLoader()加载属性文件  
  24.     public static String getPropertyByName2(String path, String name) {  
  25.         String result = "";  
  26.   
  27.         // 方法二:通过类加载目录getClassLoader()加载属性文件  
  28.         InputStream in = GetProperty.class.getClassLoader()  
  29.                 .getResourceAsStream(path);  
  30.         // InputStream in =  
  31.         // this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");  
  32.   
  33.         // 注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用  
  34.         // InputStream in =  
  35.         // Object.class.getResourceAsStream("/mailServer.properties");  
  36.         Properties prop = new Properties();  
  37.         try {  
  38.             prop.load(in);  
  39.             result = prop.getProperty(name).trim();  
  40.             System.out.println("name:" + result);  
  41.         } catch (IOException e) {  
  42.             System.out.println("读取配置文件出错");  
  43.             e.printStackTrace();  
  44.         }  
  45.         return result;  
  46.     }  
  47.   
  48. }  

action代码如下:

调用action,即可获取相应配置文件的属性值。
[java] view plaincopy
  1. package com.hsinghsu.test.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class TestAction extends ActionSupport {  
  6.   
  7.     private static final long serialVersionUID = 3348881101306356364L;  
  8.   
  9.     public String test(){  
  10.           
  11.         System.out.println("=="+GetProperty.getPropertyByName("mailServer","userName"));  
  12.           
  13.         System.out.println("==>>"+GetProperty.getPropertyByName2("mailServer.properties","userName"));  
  14.           
  15. //      //以下参数从properties文件读取  
  16. //              String mailServerHost = null; // 发送邮件的服务器的IP  
  17. //              String mailServerPort = null; // 发送邮件的服务器端口  
  18. //              String userName = null; // 登陆邮件发送服务器的用户名  
  19. //              boolean authValidate = false; // 是否需要身份验证  
  20. //                
  21. //              try {  
  22. //                  //方法一:通过java.util.ResourceBundle读取资源属性文件  
  23. //                  mailServerHost = java.util.ResourceBundle.getBundle("mailServer").getString("mailServerHost");  
  24. //                  System.out.println("mailServerHost:"+mailServerHost);  
  25. //              } catch (Exception e) {  
  26. //                  System.out.println("mailServerHost error:"+mailServerHost);  
  27. //              }  
  28. //                
  29. //              //方法二:通过类加载目录getClassLoader()加载属性文件  
  30. ////                InputStream in = TestAction.class.getClassLoader().getResourceAsStream("mailServer.properties");  
  31. //              InputStream in = this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");  
  32. //                
  33. //              //注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用  
  34. ////                InputStream in = Object.class.getResourceAsStream("/mailServer.properties");   
  35. //              Properties prop = new Properties();  
  36. //              try {  
  37. //                  prop.load(in);  
  38. //                  mailServerHost = prop.getProperty("mailServerHost").trim();  
  39. //                  mailServerPort = prop.getProperty("mailServerPort").trim();  
  40. //                  userName = prop.getProperty("userName").trim();  
  41. //                  authValidate = prop.getProperty("authValidate").trim().equalsIgnoreCase("true");  
  42. //                    
  43. //                  System.out.println("mailServerHost:"+mailServerHost+" mailServerPort:"+mailServerPort+" userName:"+userName+" authValidate:"+authValidate);  
  44. //              } catch (IOException e) {  
  45. //                  System.out.println("读取邮箱服务配置文件出错");  
  46. //                  e.printStackTrace();  
  47. //              }   
  48. //          
  49.         return null;  
  50.     }  
  51.   
  52. }  
0 0