从web工程的src目录下读取.properties文件
import org.apache.log4j.logger;import java.io.file;import java.io.ioexception;import java.io.inputstream;import java.util.properties;public class ftpurlutil{ /** * 日志组件 */ private static logger logger = logger.getlogger(ftpurlutil.class.getname()); private static ftpurlutil instance = null; /** * 用于存放上传文件的目录 */ private string uploadpath; /** * 配置文件名称 */ private string configfilestring = "ftpurl.properties"; /** * 读取文件后的结果 */ private string exeresult; /** * 只允许创建一个实例对象 */ private ftpurlutil() { } /** * 创建本类的单例 * <p/> * getinstance(单例) * * @return ftpurlutil对象 */ public static ftpurlutil getinstance() { if (instance == null) { instance = new ftpurlutil(); } return instance; } /** * 读取配置文件 * * @return boolean */ public boolean readconfigfile() { //读取配置文件 inputstream inputstream = this.getclass().getclassloader().getresourceasstream(configfilestring); //读路径出错,换另一种方式读取配置文件 if (null == inputstream) { logger.error("read config file failed."); this.exeresult = "upload_read_config_file_failed"; return false; } //读取配置文件中的appid和moduleid properties p = new properties(); try { p.load(inputstream); } catch (ioexception e1) { logger.error("load config file failed." + e1); this.exeresult = "upload_load_config_file_failed"; return false; } //得到文件路径 this.uploadpath = p.getproperty("path"); //判断文件路径是否存在 try { file filepath = new file(this.uploadpath.trim()); if (!filepath.exists()) { this.exeresult = "upload_file_path_no_exist"; return false; } } catch (exception e) { logger.error("read filepath failed." + e); this.exeresult = "upload_file_path_no_exist"; return false; } return true; } public string getexeresult() { return exeresult; } public void setexeresult(string exeresult) { this.exeresult = exeresult; } public string getuploadpath() { return uploadpath; } public void setuploadpath(string uploadpath) { this.uploadpath = uploadpath; }}