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 实现了 : ...
随机推荐
- KIDS采购销售管理系统
{KIDS采购销售管理系统} 软件项目总结报告 编号:-{kpss}-CLOSUREREPORT 版本:1.0 变更记录 1 项目信息 2 项目说明 [本项目主要用于母婴店人事管理,采购管理,商品管理 ...
- 解决.Net Core跨域问题
什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 跨域的几种情况 1.端口和协议的不同,只能通过后台来解决 2.localhost和127.0.0.1虽然 ...
- ES6使用fetch请求数据
ie是完全不支持fetch的. fetch(url,{method:"get/post"}).then(res=>{ }) 如果请求返回的status是200,body是 ...
- 环境搭建文档——Windows下的Git搭建详解
Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...
- javascript_01
- django安装及简单使用
1 web应用2 cs和bs架构3 http协议----重点 -特点:1 2 3 4 -请求头: 请求首行 get / http/1.1\r\n key:value\r\n key:value\r\n ...
- 日程管理Demo4中的bug
Demo4的github地址 模拟器有点慢之后贴图 https://git.oschina.net/annie_guo/study.git 在登陆注册界面中java的提示语言(Login.java) ...
- HTML5中input标签有用的新属性
HTML5对input增加了一些新标签,个人觉得比较常用有效的以下几个 placeholder=“请输入” 常见用于默认提示 autofocus 自动聚焦到当前输入框 maxlength=" ...
- tomcat设置错误页面
今日学习笔记: 当我们访问tomcat的一个不存在的页面,返回错误信息如下: 这样的界面直接暴露给用户并不友好,有时候还不安全,因此一般需要修改默认的错误页. vim /$TOMCAT_HOME/co ...
- Windows 10 IoT Core 17120 for Insider 版本更新
今天,微软发布了Windows 10 IoT Core 17120 for Insider 版本更新,本次更新只修正了一些Bug,没有发布新的特性.相比于17115,又少了两个已知的问题. 一些已知的 ...