JAVA Web项目获取src和WebContent目录下的配置文件
一,获取src下面的配置文件信息
1,结构图如下:
package com.binp.properties; import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle; public class GetPropertiesValues { public static void main(String[] args) throws Exception { //1,第一种方法(如果配置文件放在包下面,就需要在路径中把包的路径加上去)
String path = GetPropertiesValues.class.getResource("/").getPath(); System.out.println(path); FileInputStream fInputStream = new FileInputStream(path+"pro.properties"); Properties properties = new Properties(); properties.load(fInputStream);
System.out.println(properties.getProperty("className")); //2,第二种方法(如果配置文件放在包下面,就需要在路径中把包的路径加上去)
InputStream iStream = GetPropertiesValues.class.getResourceAsStream("/pro.properties");
properties.load(iStream);
iStream.close(); System.out.println(properties.getProperty("method")); //3,第三种方法(此方法只能将配置文件放置在src目录下,不能放在包中)
String value = ResourceBundle.getBundle("pro").getString("admin"); System.out.println(value); } }
二,获取WebContent目录下的配置文件
1,前提条件:是在tomcat启动的情况下:
@WebServlet("/testEvery")
public class testEveryServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public testEveryServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* 访问url:http://localhost:8080/demoProj/testEveryServlet
*/
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");
System.out.println("doGet读取到的/WEB-INF/config/sysconfig.properties:path:"+path);
String url = request.getSession().getServletContext().getRealPath("/WEB-INF/config/config.properties");
System.out.println("doGet读取到的/WEB-INF/config/config.properties:url:"+url);
/**
* 结果:
* doGet:path:D:\tomcat7\wtpwebapps\demoProj\config\sysconfig.properties
* doGet:url:D:\tomcat7\wtpwebapps\demoProj\WEB-INF\config\config.properties
*/
//只能获取src下面的
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/config/test.properties");
Properties prop = new Properties(); //map
prop.load(in);
String url1 = prop.getProperty("url");
System.out.println("获取到的url1:"+url1);//获取到的url1:www.baidu.com //不可获取
InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/config.properties");
Properties prop2 = new Properties(); //map
prop.load(in2);
String url2 = prop2.getProperty("url");
System.out.println("获取到的url2:"+url2);//获取到的url2:null //不可获取
InputStream in3 = this.getServletContext().getResourceAsStream("/webcontent.properties");
Properties prop3 = new Properties(); //map
prop.load(in3);
String url3 = prop3.getProperty("url");
System.out.println("获取到的url3:"+url3);//获取到的url3:null //不可获取
InputStream in4 = this.getServletContext().getResourceAsStream("/config/wcc.properties");
Properties prop4 = new Properties(); //map
prop.load(in4);
String url4 = prop4.getProperty("url");
System.out.println("获取到的url4:"+url4);//获取到的url4:null // 读取src下config包中的testJava.java
// InputStream in = ReadFile.class.getResourceAsStream("/config/testJava.java");//in为null
// byte[] a=new byte[100];
// in.read(a, 0, 900);
// System.out.println("读取src下config包中的testJava.java的输入流in的内容toString:"+in.toString());
// System.out.println("读取到的a:"+a);
String fileName3 = ReadFile.class.getResource("/config/test.properties").getFile();
System.out.println("读取src下config包中的test.properties:"+fileName3);
//输出:读取src下config包中的test.properties:/D:/tomcat7/wtpwebapps/demoProj/WEB-INF/classes/config/test.properties
// in.close(); // 读取src下 基名为myproperties的properties文件,获取其中name配置值
String value = ResourceBundle.getBundle("myproperties").getString("name");
System.out.println("获取到的myproperties.properties的值value:"+value);
//输出:获取到的myproperties.properties的值value:myname // 读取src下myproperties.properties
InputStream in1 = ReadFile.class.getResourceAsStream("/myproperties.properties");
Properties properties = new Properties();
properties.load(in1);
String value2 = properties.getProperty("name"); // 获得name属性
System.out.println("获取到的myproperties.properties的值value2:"+value2);
//获取到的myproperties.properties的值value2:myname //读取src下的
String sensitiveWordsServerPath1 = SysConfig.getSysParam("sensitiveWords_server_path1");
System.out.println("获取的sensitiveWordsServerPath1:"+sensitiveWordsServerPath1);
//获取的sensitiveWordsServerPath1:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx //读取src下的
String pp = prop("sensitiveWords_server_path1");
System.out.println("pp:"+pp);//pp:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx
} public String prop(String url){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/sysconfig.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("p:"+p);
return p.getProperty(url);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");
System.out.println("doPost:path:"+path);
} }
参考文档:https://blog.csdn.net/superit401/article/details/78206877
JAVA Web项目获取src和WebContent目录下的配置文件的更多相关文章
- 【转】Java Web 项目获取运行时路径 classpath
Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...
- Maven项目中读取src/main/resources目录下的配置文件
在Maven项目的开发中,当需要读取src/下的配置文件时,该怎么做? 我们假设Resources下有一个文件名为kafka.properties的配置文件(为什么用kafka.properties, ...
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件
路径说明: 一.加载类目录下的配置文件 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:ap ...
- java web项目获取各种路径
1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获取web项目的全路径 例如 ...
- Java web项目搭建系列之二 Jetty下运行项目
在项目pom.xml文件中添加Jetty运行配置 在pom.xml文件project节点下插入如下代码: <build> <plugins> <plugin> &l ...
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...
- web项目部署以及放到ROOT目录下
最近度过了一个国庆长假,好几天都没有写博客了! 发布这篇案例也是希望能帮助到像我一样的菜鸟o(* ̄︶ ̄*)o,百度上面的资料都不怎么全.也没有人说明注意事项.总是这篇说一点.那个人也说补一点,最后自己 ...
- java web项目获取项目路径
注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里. 1.方法一 调试时只能获取eclips ...
- Java Web 项目获取运行时路径 classpath
假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么java文件夹和resources文件夹在运行时就是class ...
随机推荐
- 密信(MeSince),将取代传统电子邮件
电子邮件发展至今已经有几十年的历史,但仍然是最重要的现代互联网应用之一.在全球范围内,每小时发送的非垃圾邮件数量超过30亿封,从工作场景的使用到个人生活,电子邮件都扮演着不可或缺的角色.但是由于明文电 ...
- sklearn学习8-----GridSearchCV(自动调参)
一.GridSearchCV介绍: 自动调参,适合小数据集.相当于写一堆循环,自己设定参数列表,一个一个试,找到最合适的参数.数据量大可以使用快速调优的方法-----坐标下降[贪心,拿当前对模型影响最 ...
- NOIP2016 DAY1 T3 换教室
换教室 Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内 ...
- [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 2 颜色和数学基础
大纲 what is color? The result of interaction between physical light in the environment and our visual ...
- JavaScript变量提升(Hoisting)的小案例
变量提升(Hoisting)的小案例 执行以下代码的结果是什么?为什么? 答案 这段代码的执行结果是undefined 和 2. 这个结果的原因是,变量和函数都被提升(hoisted) 到了函数体的顶 ...
- ssm框架下上传图片及其他信息
先引入这两个包: <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...
- C++异常注意事项
C++里面catch对于类型转换,限制比参数传递时候要多: 不可以进行标准算术转换和类的自定义转换:在函数参数匹配的过程中,可以进行很多的类型转换.但是在异常匹配的过程中,转换的规则要严厉. 标准算术 ...
- Coding上部署Ghost博客
Ghost构建于Node.js平台之上.支持0.10.*版本号的Node.js. 在你的本地计算机上执行Ghost事实上非常easy,前提是你已经安装了Node.js. 什么是Node.js? 略过 ...
- 文件类似性推断 -- SimHash
近期调研了一下simhash算法,它主要用在谷歌网页去重中.网上有非常多原理性的介绍. 既然能够用来推断文件的相似性,就想知道效果怎么样.simhash的准确度是否依赖于分词算法?是否和simhash ...
- OS - 线程和进程的差别
进程是资源分配的基本单位,又是调度执行的基本单位.比如.用户执行自己的程序,系统就创建一个进程.并为它分配资源,包含各种表.内存空间.磁盘空间.I/O设备等. 然后.把该进程放入进程的就绪队列.进程调 ...