博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java在WEB项目中获取文件路径
阅读量:7106 次
发布时间:2019-06-28

本文共 1092 字,大约阅读时间需要 3 分钟。

jsp中获得文件路径

1、根目录所对应的绝对路径:request.getRequestURI();

2、文件的绝对路径:application.getRealPath(request.getRequestURI())

3、当前web应用的绝对路径:application.getRealPath("/")

4、取得请求文件的上层目录:

newFile(application.getRealPath(request.getRequestURI())).getParent()

servlet中获得文件路径

1、根目录所对应的绝对路径:request.getServletPath()

2、文件的绝对路径:

request.getSession().getServletContext().getRealPath(request.getRequestURI())

3、当前web应用的绝对路径:servletConfig.getServletContext().getRealPath("/")

注:ServletContext对象获得几种方式:

javax.servlet.http.HttpSession.getServletContext()

javax.servlet.jsp.PageContext.getServletContext()

javax.servlet.ServletConfig.getServletContext()

java中获得文件路径

1、Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()

2、MyClass.class.getClassLoader().getResource("").toURI().getPath()

3、ClassLoader.getSystemResource("").toURI().getPath()

4、MyClass.class.getResource("").toURI().getPath()

5、MyClass.class.getResource("/").toURI().getPath()

6、new File("/").getAbsolutePath().toURI().getPath()

7、System.getProperty("user.dir").toURI().getPath()

转载于:https://www.cnblogs.com/wjw1210/p/6955292.html

你可能感兴趣的文章
Windows环境下CGAL的安装
查看>>
基本命令
查看>>
JBOSS集群和安装
查看>>
HDU-2602 Bone Collector——01背包
查看>>
[OC][地图] 高德地图之定位初探(一)
查看>>
软件测试入门随笔——软件测试基础知识(四)
查看>>
socket服务器的搭建-Mac(转)
查看>>
cocos2d的position和anchorPoint
查看>>
【数通学习】【三层交换】IP地址
查看>>
ECSHOP农行支付接口开发(含手机端)
查看>>
ios专题 - 常用设计模式
查看>>
流式标签生成控件
查看>>
Struts2拦截器总结<转>
查看>>
黄聪:php中时间轴开发,即显示为“刚刚”、“5分钟前”、“昨天10:23”等(转)...
查看>>
黄聪:C#操作xml SelectNodes,SelectSingleNode通过 xPath 定位class包含Contains的DIV
查看>>
Linq的条件查询
查看>>
0515Python基础-带参数的装饰器-多个装饰器
查看>>
QQ机器人Java版
查看>>
C#里判断字符串是否为纯数字
查看>>
C语言求字符串长度
查看>>