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对象,当然了获取路径肯定没问题
通过这次的填坑经历又一次坚定了使用linux的信心和决心,以前多次使用linux的尝试都失败了,本着不pass(怕死)的心态又一次安装了kali和linux mint双系统(原来是windows和mint,把windows格了,把心爱的linux安装在了心爱的SSD上),坚持着一个月来,感觉越来越得心应手
java web路径和spring读取配置文件的更多相关文章
- Spring 读取配置文件(二)
Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...
- Spring 读取配置文件(一)
注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...
- Java中spring读取配置文件的几种方法
Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...
- Spring读取配置文件,地址问题,绝对路径,相对路径
Spring在读取配置文件时,是相对于bin,或者WEB-INF的: “applicationContext.xml”就是找bin或WEB-INF及子文件夹下的文件: “/res/applicatio ...
- Spring读取配置文件,获取bean的几种方式
BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
- java web 加载Spring --web.xml 篇
spring是目前最流行的框架.今天谈谈对spring的认识 起步 javaweb中我们首先会遇到的配置文件就是web.xml,这是javaweb为我们封装的逻辑,不在今天的研究中.略过,下面是一个标 ...
- Java Web 学习(4) —— Spring MVC 概览
Spring MVC 概览 一. Spring MVC Spring MVC 是一个包含了 Dispatcher Servlet 的 MVC 框架. Dispatcher Servlet 实现了 : ...
随机推荐
- 消除blur属性的边框
直接设置样式为: outline:none <!DOCTYPE html> <html lang="en"> <head> <meta ...
- Timer of STM32
下面是STM32得定时器程序,分两个文件Timer.c和Timer.h /*************************************************************** ...
- POM文件详解(2)
1 项目构建 <!-- 构建项目需要的信息 --> <build> <!-- 子项目可以引用的默认插件信息.该插件配置项直到被引用时才会被解析或绑定到生命周期. ...
- 论文word排版相关插件
其中包括破解版的MathType.EndNote X7以及Aurora 链接:http://pan.baidu.com/s/1boRZTmf 密码:a6ai
- mac上terminal_问题_1117
(1)安装Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install ...
- some knowledge of the IT world
IT世界一切皆是可信息化(数据的转换)即信息记录一切,对信息的控制{存储,运算,传输{信息的位置转移},转换}就是对一切的控制{硬件(实质维)以信息的控制{软件形式(存在维)}进行操作} 信息本身的实 ...
- 了解AOP
Spring AOP的实现是基于JAVA的代理机制, 从JDK1.3开始就支持代理功能, 但是性能成为一个很大问题, 为了解决JDK代理性能问题, 出现了CGLIB代理机制.它可以生成字节码, 所以它 ...
- day_4流程控制之分支结构循环结构及for循环
复习一下昨天的内容 1:变量的命名规范 只能由数字 字母 及下划线组成 不能以数字开头 不能与系统关键字重名 _开头有特殊含义 __开头__结尾的变量是魔法变量 支持大小驼峰 ,但建议使用下划线连接语 ...
- UICollectionView添加 HeaderView FooterView
UICollectionView显示HeaderView FooterView 不如UITableView那么容易,常用会有两种做法: 1.Xib或者Storyboard 在属性一栏中设置一下: 如图 ...
- text-decoration:[ text-decoration-line ] || [ text-decoration-style ] || [ text-decoration-color ] 默认值:
css3中字体装饰,多样化的界面效果,: [ text-decoration-line ]:指定文本装饰的种类.相当于CSS2.1的 text-decoration 属性, 可取值:none | un ...