servlet中获取各种相对地址(服务器、服务器所在本地磁盘、src等)。
【本文简介】
本文将提供javaWeb中经常使用到的相对路径的获取方法,分别有:
- url基本地址
- 带目录的url地址
- 服务器的根路径
- 服务器所在的 本地磁盘路径
- 服务器所在的本地磁盘路径,带文件夹
- SRC目录下的文件的路径,带文件夹
并封装成一个工具类,以便复用。
【java代码】
package com.zjm.www.util; import javax.servlet.http.HttpServletRequest; /**
* @描述 : 获取各种相对路径的工具类
* @作者 :小M
* @博客 : http://www.cnblogs.com/xiaoMzjm/
* @时间 : 2014/07/30
*/
public class PathUtil{ /**
* 获取服务的url基本地址
* @param request 请求
* @return 例如:http://localhost:8080/test/ , 其中test为项目名
*/
public static String getServerPath(HttpServletRequest request){
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; return basePath;
}
/**
* 获取带目录的url地址
* @param request 请求
* @param folderName 文件夹名 ,例如:DownLoadFile
* @return 例如:http://localhost:8080/test/DownLoadFile
*/
public static String getServerPath(HttpServletRequest request,String folderName){
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
return basePath+folderName;
}
/**
* 获取服务器的根路径
* @param request 请求
* @return 例如:/test , 其中test为项目名
*/
public static String getServerContextPath(HttpServletRequest request){
String path = request.getContextPath();
return path;
} /**
* 获取服务器所在的 本地磁盘路径
* @param request 请求
* @return 例如:D:\D\sofe\apache-tomcat-8.0.5\webapps\test , 其中test为项目名
*/
public static String getDiskPath(HttpServletRequest request){
String path = request.getServletContext().getRealPath("/")+"\\";
return path;
}
/**
* 获取服务器所在的本地磁盘路径,带文件夹
* @param request 请求
* @param folderName 文件夹名 ,例如:DownLoadFile
* @return 例如:D:\D\sofe\apache-tomcat-8.0.5\webapps\test\DownLoadFile
*/
public static String getDiskPath(HttpServletRequest request,String folderName){
String path = request.getServletContext().getRealPath("/")+"\\";
return path+folderName;
} /**
* 获取SRC目录下的文件的路径,带文件夹
* @param folderName
* @return 例如:/F:/myEclipse2013WokeSpace/TestByServlet/WebRoot/WEB-INF/classes/test.txt
*/
public String getSRCPath(String folderName){
String path = this.getClass().getClassLoader().getResource(folderName).getPath();
return path;
}
}
【该工具类附件】
复制在浏览器打开既可下载。
http://files.cnblogs.com/xiaoMzjm/PathUtil.rar
servlet中获取各种相对地址(服务器、服务器所在本地磁盘、src等)。的更多相关文章
- Web版需求征集系统所得1,servlet中获取checkbox复选框的值
servlet中获取checkbox复选框的值 </tr> <tr> <td align="right">研究类型</td> < ...
- jsp内置对象pageContext如何在Servlet中获取值
pageContext javax.servlet.jsp.PageContext 的实例,该对象代表该JSP 页面上下文,使用该对象可以访问页面中的共享数据.常用的方法有getServletCont ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- 本地Linux虚拟机内网穿透,服务器文件下载到本地磁盘
本地Linux虚拟内网穿透 把服务器文件下载到本地磁盘 https://natapp.cn/ 1.注册账户点击免费隧道
- Servlet中获取JSP内置对象
方便自己查询,嫌低级的勿喷.... 1.request 在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是Htt ...
- Servlet中获取Spring管理的bean
描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...
- Servlet中获取POST请求的参数
在servlet.filter等中获取POST请求的参数 form表单形式提交post方式,可以直接从 request 的 getParameterMap 方法中获取到参数 JSON形式提交post方 ...
- web.xml中在Servlet中获取context-param和init-param内的参数
引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在serv ...
- servlet中获取配置文件中的参数.
web.xml (添加init-param) <?xml version="1.0" encoding="UTF-8"?> <web-app ...
随机推荐
- layui实现点击按钮添加行(方法渲染创建的table)
/ jquery实现的搜索功能 $('#search_btn').on('click',function(){ var txt=$('#inputValue').val(); var value=$( ...
- 清理iOS中的“其他”空间垃圾文件
关于如何清理 iOS 里的"其他"空间的教程,网上搜索那是一大堆,不过都是对于2010年某坛某篇"技术文"的无数次简单复制粘帖,可行性已经被各路尝试者们踩到了地 ...
- jQuery 实战读书笔记之第四章:使用特性、属性和数据
使用属性 /* 每个元素都有一或多个特性,,这些特性的用途是给出相应元素或其内容的附加信息.(出自 JavaScript 高级程序设计) */ /* 特性是固有的 JavaScript 对象 属性指的 ...
- php中while($row = $results->fetch_row())调用出错
php中while($row = $results->fetch_row())调用出错 错误处在sql语句上
- 小型web服务器thttpd的学习总结(上)
1.软件的主要架构 软件的文件布局比较清晰,主要分为6个模块,主模块是thttpd.c文件,这个文件中包含了web server的主要逻辑,并调用了其他模块的函数.其他的5个模块都是单一的功能模块,之 ...
- 【Mac + GitHub】之在另一台Mac电脑上下载GitHub的SSH链接报错
当输入git命令github项目时报错: ⇒ git clone git@github.com:/TX-Class.git Cloning into 'TX-Class'... Warning: Pe ...
- 详解 SWT 中的 Browser.setUrl(String url, String postData, String[] headers) 的用法
http://hi.baidu.com/matrix286/item/b9e88b28b90707c9ddf69a6e ———————————————————————————————————————— ...
- 我的第一个reactnative
由于在做极光推送,前端使用的框架是reactnative,后台写好后为了测试一下,所以按照react官方的教程搭了遍react. 开发环境: 1.windows 7(建议各位如果开发react的最好还 ...
- Nucleus PLUS简单介绍
近些年来,随着嵌入式系统飞速的发展,嵌入式实时操作系统广泛地应用在制造工业.过程控制.通讯.仪器仪表.汽车.船舶.航空航天.军事.装备.消费类产 品等方面.今天嵌入式系统带来的工业年产值超过了1万亿美 ...
- JVM命令行工具&垃圾收集器&垃圾收集策略思维导图
1.JVM命令行工具 2.垃圾回收算法 3.垃圾收集器