读取properties资源特性文件的值(键值对)、路径

读取资源文件中的类为:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropetiesInfo {

public final static String CONFIG = "remoting.properties";

public static Object getKeyInfo(String filePath,String keyName){
InputStream proIn = PropetiesInfo.class.getClassLoader().getResourceAsStream(filePath);
Properties pro = new Properties();
try {
pro.load(proIn);
} catch (IOException e) {
e.printStackTrace();
}
//back = pro.getProperty(keyName);
return pro.get(keyName);
}

/*
public static void main(String args[]){
System.out.println("version===="+String.valueOf(PropetiesInfo.getKeyInfo("remoting.properties", "version")));
}
*/
}

在项目的src目录下新建一个资源文件remoting.properties,如下:
ftp_host = 10.6.8.32 
ftp_port = 56
ftp_userName = hello
ftp_password =jhjdhdjkh

则现在可以通过下面的方式直接读取给定键的值:
PropetiesInfo.getKeyInfo(PropetiesInfo.CONFIG, "ftp_host")
PropetiesInfo.getKeyInfo(PropetiesInfo.CONFIG, "ftp_port")


[b]Spring配置文件中利用PropertyPlaceholderConfigurer获取properties文件中值:[/b][url]http://tangwenchao86.iteye.com/blog/1199044[/url]


[b]ClassPathXmlApplicationContext是spring读取xml中bean最常用的类[/b]
简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用:
ClassPathXmlApplicationContext cpx=new ClassPathXmlApplicationContext ("包名(或者是保的完整路径)/配置文件名字(也就是xml文件)");
cpx.getBean("配置文件中定义的bean实例");

例如:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");

如果是两个以上的xml文件:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});

或者用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");

或者:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/conf/spring/*/*.xml","classpath:/conf/spring/*/*/*.xml");
XxxService service = (XxxService)context.getBean(XxxService.class);

ClassPathXmlApplicationContext注意事项:
1.classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
2.如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;


[b]路径:[/b]
用File.separator获取系统的文件分隔符:windows系统的文件分隔符为\;linux系统为/

javax.servlet.ServletContext接口提供了获取应用的绝对路径的方法getRealPath、以及获取当前应用上下文的方法getContextPath。
可以通过以下方式获取ServletContext:
config.getServletContext() //ServletConfig config

request.getSession().getServletContext()

所以,获取应用的绝对路径:
config.getServletContext().getRealPath("")
config.getServletContext().getRealPath("/")

request.getSession().getServletContext().getRealPath("")
request.getSession().getServletContext().getRealPath("/")

config.getServletContext().getRealPath(filename) //filename这个文件是放在WebRoot目录下的

windows系统环境下:
request.getSession().getServletContext().getRealPath("") 为D:\tomcat-6.0.37\webapps\Test
request.getSession().getServletContext().getRealPath("/")为D:\tomcat-6.0.37\webapps\Test\

linux系统环境下:
request.getSession().getServletContext().getRealPath("")为 /root/aaa/installedApps/Test_war.ear/Test.war/
request.getSession().getServletContext().getRealPath("/")为/root/aaa/installedApps/Test_war.ear/Test.war

获取应用的上下文:
config.getServletContext().getContextPath()为/Test
request.getContextPath() 输出:/Test
在jsp页面中${pageContext.request.contextPath}为/Test
这个/ 指部署目录: tomcat\webapps\
在html、js等访问资源文件时,src="/resources...", 这个/ 也是指部署目录: tomcat\webapps\
如果这样设置src,说明应用系统跟资源文件分离的。两者部署在同一个tomcat下。

在windows下,路径中多若干个/或\,也可以正常运行
例如,D:\tomcat-6.0.37\webapps\resources//download/template/xxx.xls
甚至,D:\tomcat-6.0.37\webapps\resourcesdownload/template/xxx.xls
甚至,D:\tomcat-6.0.37\webapps\resources/\\\\\download/template/xxx.xls
甚至,D:\tomcat-6.0.37\webapps\resources/\\\\\///download/template/xxx.xls
甚至,D:\tomcat-6.0.37\webapps\resources/\//\//\///\\\//\download/template/xxx.xls
甚至,D:\tomcat-6.0.37\webapps\resources/\//\//\///\\\//\download\//\//\///\\\//\template\//\//\///\\\//\xxx.xls
都是可以正常运行的。
------------------------------------------
File file= new File("bsPDF");
if(!file.exists()){
file.mkdirs();
}
String path = file.getAbsolutePath(); //绝对路径
如果在web项目中,部署tomcat后运行上段代码,将在tomact的bin目录下创建一个文件夹bsPDF,此时path=D:\tomcat\bin\bsPDF
如果用Run As-->Java Application直接运行代码,则在项目的工作空间生成一个文件夹bsPDF,此时path=E:\programe\Test\bsPDF

req.getRequestURI()= /Test/aaa/xxx.html 或 /Test/aaa/xxx.do
req.getRequestURL()= http://10.3.1.116:8080/Test/aaa/xxx.html 或 http://10.3.1.116:8080/Test/aaa/xxx.do
----------------------------------------------------------
在.js文件中不能直接访问jsp文件中通过jstl <c:set>标签定义的变量。
但是下例为什么可以正常运行?
ajax中url: '${_ctPath}/xxx_xxxxx.do'

但是ajax中作为参数data中a="${_ctPath}"又不行了
why...
-------------------------------------------------------------------
采用../../这样的相对路径 或者 绝对路径/Test/pages/...
css <link...
js <script...
html中,<img src="."
请求, .do

以webRoot为准:
引入jsp <%@ include file="/pages/xxx/xxx.jsp" %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值