java web路径和spring读取配置文件

本文探讨了在JavaWeb环境中如何正确处理不同操作系统间的路径差异,并详细介绍了如何使用Spring框架来读取配置文件,包括XML配置及代码级读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java web路径和spring读取配置文件

此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题

  • 找不到自定义的property配置文件
  • 上传图片的时候找不到路径

开发的时候是在windows上的,运行正常,部署的时候就出问题了,肯定是windows和linux路径区别导致的(一个小问题来回鼓捣了几个小时,因为有自己对windows下和linux下的区别还不是特别了解,还有就是每次在windows下修改完成以后都要重新上传到阿里云,项目较大来回也需要较多时间。。。),遂决定好好看看java web路径的问题。

普通java程序获取路径

复制代码
Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath()
null

Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
/D:/workspace/EPEducationManager/build/classes/

UserResource.class.getClassLoader().getResource("/").toURI().getPath()
null

UserResource.class.getClassLoader().getResource("").toURI().getPath()
/D:/workspace/EPEducationManager/build/classes/

UserResource.class.getResource("").toURI().getPath()
/D:/workspace/EPEducationManager/build/classes/com/phy/em/user/rest/

UserResource.class.getResource("/").toURI().getPath()
/D:/workspace/EPEducationManager/build/classes/

System.getProperty("user.dir")
D:\workspace\EPEducationManager

在java web中获取路径

复制代码
Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath()
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/EPEducationManager/WEB-INF/classes/

Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
/C:/tomcat7/lib/

UserResource.class.getClassLoader().getResource("/").toURI().getPath()
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/EPEducationManager/WEB-INF/classes/

UserResource.class.getClassLoader().getResource("").toURI().getPath()
/C:/tomcat7/lib/

UserResource.class.getResource("").toURI().getPath()
/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/EPEducationManager/WEB-INF/classes/com/phy/em/user/rest/

UserResource.class.getResource("/").toURI().getPath()
/C:/tomcat7/lib/

System.getProperty("user.dir")
C:\Program Files (x86)\eclipse
复制代码

根据上面的输出选择对应的获取路径的方法,特别注意获取得到的path前面有"/",不要手贱删除"/",对,我就是那个手贱的人,删除了"/",因为看到前面有斜杠在windows资源管理器中是打不开的,我就删除了,结果在windows上运行是正确的,但是部署在linux上的时候把"/"删除了就成了"var/share/lib",明显这个录警示不正确的,本来是根目录下var...成了当前目录下var...

获取路径就可以读取制定目录下的配置文件了

使用spring读取配置文件

在xml中读取
复制代码
<bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
  <property name=”location”>
    <value>/WEB-INF/configInfo.properties</value>
  </property>
  <property name=”fileEncoding” value=”utf-8″ />
</bean>
复制代码

在xml中使用

复制代码
<property name=”host”>
  <value>${email.host}</value>
</property>
<property name=”port”>
  <value>${email.port}</value>
</property>
复制代码

通过以上两步就可以完成在读取property配置文件并注入到对应的bean中,但是有时候我们并不需要为了读取配置而创建一个bean,我们只想代码中直接读取配置文件,可以使用如下的方式

ResourceUtils.getFile("classpath:config.properties").getPath()
D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\EPEducationManager\WEB-INF\classes\

可以直接在代码中使用"classpath"来定位配置文件,获取得到的是一个File对象,当然了获取路径肯定没问题




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值