1. 程式人生 > >Java類建立公共類獲取配置檔案.properties配置的值

Java類建立公共類獲取配置檔案.properties配置的值

建立的SystemConstants 類:
(紅色部分為核心程式碼)
package cn.test.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class SystemConstants {
	
	
	/**
	 * 檔案伺服器引數配置
	 */
	public static  String SFTP_host;
	public static  String SFTP_port;
	public static  String SFTP_username;
	public static  String SFTP_password;
	public static  String SFTP_directory;
	public static  String SFTP_httpBaseUrl;
	
	static {
		Properties sysprop = new Properties();
		try {
			sysprop.load(new InputStreamReader(SystemConstants.class
					.getResourceAsStream("/default.properties"),"UTF-8")); 
			
				
			//檔案上傳伺服器引數
			SFTP_host = sysprop.getProperty("SFTP_host");
			SFTP_port=sysprop.getProperty("SFTP_port");
			SFTP_username = sysprop.getProperty("SFTP_username");
			SFTP_password = sysprop.getProperty("SFTP_password");
			SFTP_directory= sysprop.getProperty("SFTP_directory");
			SFTP_httpBaseUrl= sysprop.getProperty("SFTP_httpBaseUrl");
			//調微信介面服務配置
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

	public static String getSFTP_host() {
		return SFTP_host;
	}

	public static void setSFTP_host(String sFTP_host) {
		SFTP_host = sFTP_host;
	}

	public static String getSFTP_port() {
		return SFTP_port;
	}

	public static void setSFTP_port(String sFTP_port) {
		SFTP_port = sFTP_port;
	}

	public static String getSFTP_username() {
		return SFTP_username;
	}

	public static void setSFTP_username(String sFTP_username) {
		SFTP_username = sFTP_username;
	}

	public static String getSFTP_password() {
		return SFTP_password;
	}

	public static void setSFTP_password(String sFTP_password) {
		SFTP_password = sFTP_password;
	}

	public static String getSFTP_directory() {
		return SFTP_directory;
	}

	public static void setSFTP_directory(String sFTP_directory) {
		SFTP_directory = sFTP_directory;
	}

	public static String getSFTP_httpBaseUrl() {
		return SFTP_httpBaseUrl;
	}

	public static void setSFTP_httpBaseUrl(String sFTP_httpBaseUrl) {
		SFTP_httpBaseUrl = sFTP_httpBaseUrl;
	}
	
	
}
default.properties檔案:
SFTP_host=192.9.9.9
SFTP_port=22
SFTP_username=admin
SFTP_password=123
SFTP_directory=/usr/local//upload/image/
SFTP_httpBaseUrl=http://192.9.9.9:8080/upload/image/