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


1获取webcontent路径下的配置文件
String path = ParseReader.class.getResource("/").getPath();
String websiteUrl = path.replace("/build/classes", "").replace("%20", " ")
					+ "WebContent/WEB-INF/";
System.out.println(websiteUrl);

打印的结果为/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();
		}
	}
}

javaweb工程获取webContent下WEB-INF下的配置文件_第1张图片

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);
	}

}




你可能感兴趣的:(前端相关)