web应用加载资源文件

来源:互联网 发布:刷天猫搜索流量软件 编辑:程序博客网 时间:2024/05/22 10:33

web应用加载资源文件


"."    表示相对路径中的当前路径。相对于java命令运行的目录。

结论:
用myeclipse工具开发java项目中, "."代表在java项目的根目录下MyEclipse工作空间/bin 目录开始
在web项目中,"."代表在tomcat/bin目录下开始,所以不能使用这种相对路径。

/   斜杠表示classpath的根目录

          在java项目下,classpath的根目录从bin目录开始
          在web项目下,classpath的根目录从WEB-INF/classes目录开始

1.给服务器使用。/表示在当前web应用的根目录下(WebRoot下面)
2.给浏览器使用。/表示在webapps的根目录下

使用web加载资源文件的推荐方式

方式1:String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");   读取,返回资源文件 的绝对路径。
方式2:InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");读取,返回资源文件的输入流

在web项目Servlet中用“/”方式获取资源
Demo:
package com.cn.servlet;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Properties;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/*** Author:Liu Zhiyong(QQ:1012421396)* Version:Version_1* Date:2016年11月29日22:35:03* Desc:"."表示相对路径中的当前路径。相对于java命令运行的目录。结论:用myeclipse工具开发java项目中, "."代表在java项目的根目录下开始。在web项目中,"."代表在tomcat/bin目录下开始,所以不能使用这种相对路径。1.给服务器使用。/表示在当前web应用的根目录下(WebRoot下面)2.给浏览器使用。/表示在webapps的根目录下*/public class ResourceDemo extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* * 在java项目中这样获取,但是web项目中,这样获取不行。 *  *//*File file = new File("./src/db.properties");FileInputStream in = new FileInputStream(file);*//*File file = new File(".");System.out.println(file.getAbsolutePath());//web项目中“.”代表的位置是tomcat服务器的bin目录下*//*File file = new File("../webapps/test/WEB-INF/classes/db.properties");System.out.println(file.getAbsolutePath());//这里代表的位置是tomcat服务器的bin目录的上一级目录下的\webapps\test\WEB-INF\classes\db.propertiesFileInputStream in = new FileInputStream(file);*//** * 使用web加载资源文件的推荐方式 *//* * 方式1:this.getServletContext().getRealPath  读取,返回资源文件 的绝对路径。 *//*String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");System.out.println(path);//G:\IT_ConfigurationFile\apache\apache-tomcat-7.0.73\webapps\test\WEB-INF\classes\db.propertiesFile file = new File(path);FileInputStream in = new FileInputStream(file);*//* * 方式2:this.getServletContext().getResourceAsStream 读取,返回资源文件的输入流 */InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");Properties prop = new Properties();//读取资源文件prop.load(in);String user = prop.getProperty("user");String password = prop.getProperty("password");System.out.println("user=" + user);System.out.println("password=" + password);}}
资源文件:

 
在web项目中普通java文件中用“.”方式获取资源
 例如:   
File file = new File("../webapps/test/WEB-INF/classes/db.properties");System.out.println(file.getAbsolutePath());//这里代表的位置是tomcat服务器的bin目录的上一级目录下的\webapps\test\WEB-INF\classes\db.properties
0 0
原创粉丝点击