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 实现了 : ...
随机推荐
- 去掉"You are running Vue in development mode"提示
vue项目中报错: You are running Vue in development mode.Make sure to turn on production mode when deployin ...
- 解决更新ssh后在/etc/init.d下无sshd的问题
1.将远程服务器的/etc/init.d/ssd 文件拷贝到本地 scp /etc/init.d/ssh root@IP地址:/etc/init.d 2.vi /etc/init.d/sshd 3 ...
- 1013. Battle Over Cities 用dfs计算联通分量
使用一个标记数组,标记 节点是否已访问 int 连通度=0 dfs(node i) {标记当前节点为以访问 for(每一个节点) {if(当前几点未访问 并且 从i到当前节点有直接路径) dfs(当前 ...
- Unity自动生成AnimatorController
上一篇写了如何自动切割动画,这一篇写如何自动生成AnimatorController. 之前网上查了很多资料,看的一直很蒙,看不懂是怎么回事的,这里我先给大家明确几个概念: 画的不好,大家将就着看,写 ...
- cordova 问题汇总
用chrome进行调试: https://jingyan.baidu.com/album/db55b609fde96d4ba30a2fa9.html?picindex=8 http://rensann ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- 《java从入门到精通》学习记录
目录 <Java从入门到精通>学习记录 3 基础的基础部分: 3 一. 常量与变量 3 1. 掌握: 3 (1) .常量与变量的声明方式: 3 (2) .变量的命名规则: 3 (3) .变 ...
- Nginx 教程(1):基本概念
简介 嗨!分享就是关心!所以,我们愿意再跟你分享一点点知识.我们准备了这个划分为三节的<Nginx教程>.如果你对 Nginx 已经有所了解,或者你希望了解更多,这个教程将会对你非常有帮助 ...
- SQL Server Integration Services SSIS最佳实践
SQL Server Integration Services Best Practices Tips https://www.mssqltips.com/sql-server-tip-categor ...
- Stacking Plates(存档待续.....(没有写思路和程序))
问题描述 盘子装运公司是一家网络零售商,顾名思义,是一家只销售盘子的公司.该公司销售的盘子由不计其数的生产厂商提供,品种是全宇宙最多的,为此公司的员工倍感自豪. 在最近的一次成本分析中,公司员工发现, ...