Java Web项目中配置文件的加载路径,普通类和Servlet的默认根路径有些不一样
1、配置文件方在src下,在普通class文件中加载
public String sayHello(String name)
{
String path = "config/jdbc_mysql.properties";
Properties props = new Properties();
String error = "";
String context = "";
try{
//获得当前类加载的根目录
context = AccountService.class.getClassLoader().getResource("").toURI().getPath();
//将文件读入文件输入流,存入内存
FileInputStream fis = new FileInputStream(new File(context + path));
//加载文件流的属性
props.load(fis);
}catch(URISyntaxException e){
error +="非法URI";
e.printStackTrace();
}catch(FileNotFoundException e){
error +="找不到文件";
}catch(IOException e){
error +="配置文件加载异常";
}
String driver = props.getProperty("driverClassName");
String user = props.getProperty("user");
return "Hello World " + name +","+ context +","+ driver+"," + user+error;
}
2、在Servlet中加载
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//out.append("Served at: ").append(request.getContextPath());
out.write("Served at: " + request.getContextPath());
String propertiesPath = "mysql.properties";
String propertiesPath2 = "WEB-INF/classes/config/jdbc_mysql.properties";
//与上面只是少了WEB-INF/classes/
String propertiesPath3 = "config/jdbc_mysql.properties";
Properties props = new Properties();
Properties props2 = new Properties();
Properties props3 = new Properties();
//获取Servlet上下文的绝对路径
String servletContextPath = this.getServletContext().getRealPath("");
try{
//将文件读入输入流,存入内存
FileInputStream fis = new FileInputStream(new File(servletContextPath + propertiesPath));
//加载文件流的属性
props.load(fis);
}catch(FileNotFoundException e){
out.write("<br>");
out.write("找不到WebContent下的配置文件");
}catch(IOException e){
out.write("WebContent直接下属的配置文件加载失败");
}
try{
FileInputStream fis2 = new FileInputStream(new File(servletContextPath +propertiesPath2));
props2.load(fis2);
}catch(FileNotFoundException e){
out.write("<br>");
out.write("找不到src路径下的配置文件<br/>");
}
out.write("<br/>Servlet上下文:" + servletContextPath+"<br/>");
String haesoadb_user = props.getProperty("haesoadb_user");
String driver = props2.getProperty("driverClassName");//driverClassName
out.write("haesoadb_user:"+ haesoadb_user);
out.write("<br/>");
out.write("driver:"+driver);
String classPath ="";
try{
classPath = TestServlet.class.getClassLoader().getResource("").toURI().getPath();
FileInputStream fis3 = new FileInputStream(new File(classPath +propertiesPath3));
props3.load(fis3);
}catch(Exception e){//URISyntaxException
out.write("<br/>获取路径失败");
}
out.write("<br>通过class获取的绝对路径:" + classPath);
String user = props3.getProperty("user_test");
out.write("<br/>通过class读取配置文件的值:" +user);
}
说明String servletContextPath = this.getServletContext().getRealPath("");得到的相对于应用的路径是WebContent,也就是说,如果在WebContent下创建了文件夹或配置文件,这里加上对应文件夹或配置文件路径即能加载,和在WebContent下创建index.jsp文件,用应用名或再加上jsp文件名访问是一样的。在截图中第1个红框的配置文件在src下,成功加载到数据,此处的相对路径和前面一样,只是根目录路径不同。这和Servlet的原理有关,用户定义的Servlet经过编译会在专门的路径下生产class文件。可以看到Servlet中一样可以使用class的方式获取配置文件路径并加载,只是和使用Servlet的根路径不同而已。
在Java工程中加载配置文件路径有些差异
在本地开发时,在总项目下建目录,并将配置文件放入其中,那么使用fis = new FileInputStream("config/config.properties");即可加载配置文件,本地运行OK,但是打成jar包,使用Windows的CMD运行或放到Linux服务器上运行却发现一个奇怪的现象,报错,找不到配置文件
经过改造,在src目录下创建目录,并将配置文件放入其中,使用类名.class.getClassLoader().getResourceAsStream("config/config.properties");能够成功加载到文件,并且本地运行OK,打成jar包,用CMD或放到Linux上运行,配置文件加载OK,如图
这个问题可能和编译路径有关,仔细观察jar包会发现,文件路径并不是和Eclipse的文件路径一致,src中的源文件经编译放入了bin目录,若配置文件放在src下,则也被放入bin目录,而打成jar包的文件就是bin目录下的文件,因此若建在项目根下,则不会打进jar包中,自然加载不到