spring中获取当前项目的真实路径
总结:
方法1:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
String realPath = servletContext.getRealPath(File.separator);//G:\ty\tomcat\tomcat-7080\webapps\crm-timer
方法2:
ClassLoader classLoader = this.getClass().getClassLoader();
URL resource1 = classLoader.getResource("");
resource1.getPath();///G:/ty/tomcat/tomcat-7080/webapps/crm-timer/WEB-INF/classes/ //或者
URL resource = classLoader.getResource("/");
String path = resource.getPath();///G:/ty/tomcat/tomcat-7080/webapps/crm-timer/WEB-INF/classes/
注意:上面的方法除了 URL resource1 = classLoader.getResource(""); 外,都需要在tomcat中启动项目后才会有正常值,在junit或者main方法测试的时候都是会报错的,即使getResource("")不报错,返回的地址是这样:G:/ty/wkty2/crmCust-parent/crm-timer/bin/main/
。。。。。。参考如下。。。。。。。
方法1:
转:
spring中获取当前项目的真实路径
如果为体现项目的层次感,当我们使用request的时候,一般都是在控制层中使用的,没有必要将request也传递到service使用,所以首先是讲一下在controller获取当前的项目的绝对路径
public Message<Object> uploadImgsFile(HttpServletRequest request) {
//
String realPath = request.getSession().getServletContext().getRealPath(File.separator);
System.out.println(realPath);
//
String realPath2 = request.getServletContext().getRealPath(File.separator);
System.out.println(realPath2);
return null;
}
如果当前想要获取当前项目的绝对路径,而此时又不在controller,同时又不想传递request的参数到service层,可以这样操作
//代码片段
//现获取当前的路径
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
String realPath = servletContext.getRealPath(File.separator);
System.out.println(realPath);
经过上面的操作可以获取项目的路径,以此来方便自己项目的需求
。。。。。。。。。。。。。。。。
方法2:
转:
在java类中获取WEB-INF路径
把java项目部署到tomcat中以后,类文件的路径都在“apache-tomcat-7.0.69\webapps\APP\WEB-INF\classes”中,如果我们需要在某个java类中获取WEB-INF下的某些资源,例如现在我需要在WEB-INF下的templates文件夹下的模板资源

就需要在java文件中这样获取classes目录,然后转到templates目录下。
private static String templatePath = TemplateFactory.class.getClassLoader().getResource("/").getPath().replace("classes", "templates");
其中TemplateFactory是我的Java类名,
TemplateFactory.class.getClassLoader().getResource("/").getPath()
是获取到classes文件夹下的路径,就是apache-tomcat-7.0.69\webapps\APP\WEB-INF\classes,
然后可以把classes替换为我们想要得到的文件夹名字。
spring中获取当前项目的真实路径的更多相关文章
- JavaScript中获取当前项目的绝对路径
近期在做JavaWeb项目相关的东西,差点儿每天都遇到非常多问题,主要是由于自己对JavaWeb方面的知识不是非常清楚,尽量把自己在项目中遇到的问题都记录下来,方便以后查阅. 在我们的项目中有这种须要 ...
- Spring中获取web项目的根目录
spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器,正确地使用它们可以完成一些特定需求的功能; WebAppRootListe ...
- controller中获取当前项目物理绝对路径
用HttpServletRequest request获取 request.getSession().getServletContext().getRealPath(""); 上面 ...
- Spring MVC中获取当前项目的路径
Spring MVC中获取当前项目的路径 在web.xml中加入以下内容 <!--获取项目路径--> <context-param> <param-name>web ...
- spring中获取dao或对象中方法的实例化对象
spring中获取dao的中方法的实例化对象: //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quo ...
- 获取 web 项目的绝对路径
获取 web 项目的绝对路径 <% String path = request.getContextPath(); String basePath = request.getScheme()+& ...
- IOS中获取各个文件的目录路径的方法和NSFileManager类
转自:http://blog.sina.com.cn/s/blog_5fb39f910101di92.html IOS中获取各种文件的目录路径的方法 iphone沙箱模型的有四个文件夹,分别是什么,永 ...
- Spring中获取被代理的对象
目录 Spring中获取被代理的对象 获取Spring被代理对象什么时候可能会用到? Spring中获取被代理的对象 Spring中获取被代理的对象 ### 获取Spring被代理对象的JAVA工具类 ...
- 【poi】用POI新建一个xlsx文件【或者说将数据存入到xlsx中】/【将数据从xlsx中获取到项目中】
第一部分:写入xlsx中 使用POI创建一个xlsx文件: 项目结构如下: 具体使用的POI中的 XSSFWorkbook xlsx对象 Sheet 工作簿对象 Row 行对象 Cell 单元格 ...
随机推荐
- centos7根分区扩容(亲测有效)
root@haojftest:~# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos_test2-root 28G 14G 15G % / devtmpfs ...
- python数学第七天【期望的性质】
- fastjson与各类型的转换
参考:https://www.cnblogs.com/ceshi2016/p/7381478.html http://www.cnblogs.com/goody9807/p/4244862.html ...
- sql查询表中重复数据个数
select train_code,count(1) from tb_ask_trainbodyroadtrain group by train_code having count(1) >1
- cookie的域,路径
Cookie 的路径以及 Cookie 域 cookie 路径 cookie 一般都是由于用户访问页面而被创建的,可是并不是只有在创建 cookie 的页面才可以访问这个cookie.在默认情况下,出 ...
- Reading Text from Images Using C#
Introduction By using Optical Character Recognition (OCR), you can detect and extract handwritten an ...
- 51-node-1649齐头并进(最短路)
题意:中文题,没啥坑点: 解题思路:这道题一开始以为要跑两个最短路,后来发现不用,因为如果给定了铁路的线路,那么,公路一定是n个节点无向图的补图,所以,铁路和公路之间一定有一个是可以直接从1到n的,我 ...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK问题解决
Maven构建项目报错: 解决办法: 1.eclipse菜单 - Window - Preferences- Java - Installed JREs 将配置的JRE定位到JDK,例如JRE ho ...
- Nginx 4层反向代理
L112 是基于TCP POST_ACCEPT阶段 在建立连接后所做的事情 PREACCESS阶段 limit_conn 限流 与HTTP类似 ACCESS阶段 类似HTTP模块用于控制访问权限 S ...
- Tomcat的四种web应用部署方式详解
在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件(%Tomcat_Home%\conf\Cat ...