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

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

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

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

普通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...web

获取路径就能够读取制定目录下的配置文件了spring

使用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中使用ubuntu

<property name=”host”>
  <value>${email.host}</value>
</property>
<property name=”port”>
  <value>${email.port}</value>
</property>

经过以上两步就能够完成在读取property配置文件并注入到对应的bean中,可是有时候咱们并不须要为了读取配置而建立一个bean,咱们只想代码中直接读取配置文件,可使用以下的方式windows

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

 

能够直接在代码中使用"classpath"来定位配置文件,获取获得的是一个File对象,固然了获取路径确定没问题tomcat


 

 

经过此次的填坑经历又一次坚决了使用linux的信心和决心,之前屡次使用linux的尝试都失败了,本着不pass(怕死)的心态又一次安装了kali和linux mint双系统(原来是windows和mint,把windows格了,把心爱的linux安装在了心爱的SSD上),坚持着一个月来,感受愈来愈驾轻就熟服务器

相关文章
相关标签/搜索