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 ...
随机推荐
- VUE:路由
VUE:路由 一.说明 1)官方提供的用来实现SPA的vue插件 2)github:https://github.com/vuejs/vue-router 3)中文文档:http://router.v ...
- Ajax原理-重点
Ajax原理 AJAX即“Asynchronous Javascript And XML”,是指一种创建交互式网页应用的网页开发技术.AJAX 是一种用于创建快速动态网页的技术.它可以令开发者只向服务 ...
- React 中的 refs的应用
React Refs React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上. 这个特殊的属性允许你引用 render() 返回的相应的支撑实例( back ...
- js手动定时清除localStorage
<script type="text/javascript"> // 假设要保存变量 a 的值,过期时间为 3600秒 // 保存值 var obj = new Obj ...
- Linux学习总结(15)——提高 Vim 和 Shell 效率的 9 个建议
你上一次使用 CAPSLOCK 键是什么时候?很久没有了对不对?噢,我也是,它已经被遗忘了,它浪费了键盘上一个黄金位置.让我们把它重映射成 Control 键来发挥它的作用吧!这里告诉了你在不同的操作 ...
- Javascript 实现锚点(Anchor)间平滑跳转
(function($){ $.fn.scroller = function(options){ var defaultVal = { duration: }; var obj = $.extend( ...
- 【POJ 1850】 Code
[POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az ...
- poj2492--A Bug's Life(并查集变形)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28703 Accepted: 9350 De ...
- 【struts2】struts2中的流接收与流发送
[前言]在我们的struts2后端中,实现流的接收和发送.就能够实现向server传视频流以及下载图片. [流接收] 如今举一个传公钥的样例.struts2用一个action接收Key,而Key就是用 ...
- leveldb学习:sstable(2)
block写入:block_builder block.h和.cc里定义了block的entry存储格式和restart,提供了entry的查找接口以及迭代器.那么怎样往写block里写entry呢? ...