javaweb工程获取webContent下WEB-INF下的配置文件


1获取webcontent路径下的配置文件

<span style="font-size:18px;">String path = ParseReader.class.getResource("/").getPath();
String websiteUrl = path.replace("/build/classes", "").replace("%20", " ")
					+ "WebContent/WEB-INF/";
System.out.println(websiteUrl);</span>

打印的结果为/D:/workspace/com.phy.smart.pipe.monitor/WebContent/WEB-INF/

/D:/workspace/com.phy.smart.pipe.monitor是我当前的工程本地地址

以上思路是根据ParseReader类的加载获取到当前工程的地址,这里/build/classes是web工程在tomcat部署之后编译的.class文件路径,要替换为空,%20为空格,%20 其实就是空格,序列化之后就变成%20了%20 其实就是空格,序列化之后就变成%20了

然后就可以任意获取你webcontent下的文件路径了。

2.获取src路径下的配置文件

public class PropertiesReader {
	public static String USERNAME = "nancht";
	public static String PASSWD = "VprJG8#h==";
	private static Properties props = new Properties();

	static {
		try {
			InputStream in = new ClassPathResource("/ws.properties")
					.getInputStream();
			props.load(in);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3。以下总结三种方法读取:

//用servletContext读取web工程不同位置的资源文件
public class ServletDemo11 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		test5();
	}
	
	//读取src下面的配置文件
	private void test5() throws IOException {
		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
		Properties prop = new Properties();  //map
		prop.load(in);
		
		
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);
	}
	
	//读取src下面的配置文件(传统方式不可取)
	private void test4() throws IOException {
		FileInputStream in = new FileInputStream("classes/db.properties");
		System.out.println(in);
	}
	
	
	//读取配置文件的第三种方式
	private void test3() throws IOException {
		ServletContext context = this.getServletContext();
		URL url = context.getResource("/resource/db.properties");
		InputStream in = url.openStream();
		
	}
	

	//读取配置文件的第二种方式
	private void test2() throws IOException {
		ServletContext context = this.getServletContext();
		String realpath = context.getRealPath("/db.properties");  //c:\\sdsfd\sdf\db.properties
		
		//获取到操作文件名   realpath=abc.properties
		String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
		System.out.println("当前读到的文件是:" + filename);
		
		
		FileInputStream in = new FileInputStream(realpath);
		Properties prop = new Properties();
		prop.load(in);
		
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println("文件中有如下数据:");
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);
		
	}
	
	
	//读取配置文件的第一种方式
	private void test1() throws IOException {
		
		ServletContext context = this.getServletContext();
		InputStream in = context.getResourceAsStream("/db.properties");
		
		Properties prop = new Properties();  //map
		prop.load(in);
		
		
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值