servlet读取资源配置文件
来源:互联网 发布:华林证券软件 编辑:程序博客网 时间:2024/05/22 07:02
servlet读取资源配置文件
1. ServletContext.getResource
先在项目的src/main/resource/目录下创建一个my.properties文件,文件内容如下
name=value
在Servlet中通过下面方式获取资源文件
// 第一种方法getResourceServletContext ctx = this.getServletContext();URL url = ctx.getResource("WEB-INF/classes/my.properties");InputStream in = url.openStream();
- getResource方法返回一个URL对象,由于编译时maven会将资源文件resource目录下的文件放置在
WEB-INF/classes
目录下,所以文件地址定向到WEB-INF/classes/my.properties
- getResource方法的参数是文件相对于应用目录的位置,所以这里我们填写从
WEB-INF
开始的路径即可
2. ServletContext.getResourceAsStream
第二种方法相对于第一种方法更直接,直接获取输入流
ServletContext ctx = this.getServletContext();InputStream in = ctx.getResourceAsStream("WEB-INF/classes/my.properties");
通常情况用这种方式即可
3. ServletContext.getRealPath
这种方式是通过ServletContext.getRealPath根据文件的相对路径获取绝对路径,然后通过File对象读取
File file = new File(ctx.getRealPath("WEB-INF/classes/my.properties"));InputStream in = new FileInputStream(file);
通过上面三种方法获取了文件输入流,下面就可以通过java提供的Properties对象来对文件进行解析了。这里封装了一个方便读取的类方法:
package com.dotgua.demo1;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class GeneralUtil { public static String getPropertyValue(InputStream in, String name) { Properties p = new Properties(); try { p.load(in); } catch (IOException e) { System.out.println("Error happen, cant load property file"); e.printStackTrace(); } return p.getProperty(name); }}
在Servlet中就可以调用该方法来读取文件信息了:
String propertyValue = GeneralUtil.getPropertyValue(in, "name");
下面是完整代码
/** * ReadResource类 */package com.dotgua.demo1;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.net.URL;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ReadResource extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext ctx = this.getServletContext(); // 第一种方法getResource URL url = ctx.getResource("WEB-INF/classes/my.properties"); InputStream in = url.openStream(); // 第二种方法更直接getResourceAsStream in = ctx.getResourceAsStream("WEB-INF/classes/my.properties"); // 获取文件的绝对路径用file加载 File file = new File(ctx.getRealPath("WEB-INF/classes/my.properties")); in = new FileInputStream(file); String propertyValue = GeneralUtil.getPropertyValue(in, "name"); PrintWriter pw = resp.getWriter(); pw.println("propertyValue of 'name' = "+ propertyValue); pw.close(); }}
0 0
- servlet读取资源配置文件
- ServletContext读取资源配置文件
- 读取web中的资源配置文件
- 读取web中的资源配置文件
- Spring Resource 读取资源配置文件
- 简单的JDBC数据库连接池、读取资源配置文件
- jar读取资源配置文件,jar包内包外,以及包内读取目录的方法
- servlet读取文件
- servlet读取properties文件
- log4j.properties资源配置文件
- java中的资源配置文件
- Struts2 资源配置文件国际化
- Struts2 资源配置文件国际化
- Unity3D自定义资源配置文件
- 使用ResourceBundle 读取资源配置信息
- spring boot框架学习之重要注解3注解方式读取外部资源配置文件
- servlet读取数据库图片文件
- Servlet读取properties资源文件
- 安卓 OKHTTP 简单示例
- 疑问:
- Setting 的首页加载过程
- 用堆实现优先级队列--Java
- Matplotlib安装教程
- servlet读取资源配置文件
- 自定义View
- Scala(一)之“字符串”
- iOS view旋转一定角度
- 一天一天算日期~
- Android编译系统详解(二)——命令执行流程
- 排序总结
- 统计文本单词数
- Linux——umask使用详解