程序中的很多地方需要用到request.getRealPath()或者getServletContext.getRealPath()。这个方法受到war 和non-war的影响,以及不同app server实现的影响,返回的结果往往不一样,在weblogic中会返回null。
一般的应用都会有一个或几个配置文件放在web-inf下面,getRealPaht返回null是读配置文件有很大困难。
/**
* 通过上下文来取工程路径
*
* @return
* @throws Exception
*/
private String getAbsolutePathByContext() throws Exception {
String webPath = this.getServletContext().getRealPath("/");
webPath = webPath.replaceAll("[\\\\\\/]WEB-INF[\\\\\\/]classes[\\\\\\/]?", "/");
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
webPath = webPath.replaceAll("%20", " ");
if (webPath.matches("^[a-zA-Z]:.*?$")) {
} else {
webPath = "/" + webPath;
}
webPath += "/";
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
return webPath;
}
/**
* 通过类路径来取工程路径
*
* @return
* @throws Exception
*/
private String getAbsolutePathByClass() throws Exception {
String webPath = this.getClass().getResource("/").getPath().replaceAll("^\\/", "");
webPath = webPath.replaceAll("[\\\\\\/]WEB-INF[\\\\\\/]classes[\\\\\\/]?", "/");
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
webPath = webPath.replaceAll("%20", " ");
if (webPath.matches("^[a-zA-Z]:.*?$")) {
} else {
webPath = "/" + webPath;
}
webPath += "/";
webPath = webPath.replaceAll("[\\\\\\/]+", "/");
return webPath;
}
private String getAbsolutePathByResource() throws Exception {
URL url = this.getServletContext().getResource("/");
String path = new File(url.toURI()).getAbsolutePath();
if (!path.endsWith("\\") && !path.endsWith("/")) {
path += File.separator;
}
return path;
}
public String init() throws ServletException {
String webPath = null;
try {
webPath = getAbsolutePathByContext();
} catch (Exception e) {
}
// 在weblogic 11g 上可能无法从上下文取到工程物理路径,所以改为下面的
if (webPath == null) {
try {
webPath = getAbsolutePathByClass();
} catch (Exception e) {
}
}
if (webPath == null) {
try {
webPath = getAbsolutePathByResource();
} catch (Exception e) {
}
}
System.out.println(webPath);
return webPath;
}
在weblogic里面war通过getAbsolutePathByContext()方法获取路径为空 ,是很正常的
对一个打包的应用来说,是没有RealPath的概念的,调用getRealPath只会简单地返回null
所以可以通过上面的通过类路径来取工程路径
分享到:
相关推荐
weblogic下转换oracle 的clob类型经常报转型错误,本例子通过反射解决weblogic.jdbc.wrapper.Clob_oracle_sql_CLOB转型成oracle.sql.CLOB问题
weblogic相关jar包合集,包含weblogic.jar、wlclient.jar、wls-api.jar、api.jar,需要用到的可以下载
Oracle WebLogic Server 11g下载 Oracle WebLogic Server 10.3.1 包括两个文件: oepe11_ccjk_wls1031_linux32.bin oepe11_wls1031.exe
OracleWeblogic11g基础知识.ppt
OracleWeblogic11g基础知识.pptx
很难找的weblogic支持oracle的blob的驱动包
Weblogic Patch Set Update 10.3.6.0.211019
weblogic.xml配置详解.txt
weblogic.jdbc.vendor.oracle.OracleResultSet 解决weblogic数据库连接池存储大字段所需包
weblogic 补丁工具opatch13.9.4.2.5
weblogic installation doc version 1.0.docx 1. Oracle Linux system Install finished. 2. create oracle user groupadd dba -g 1000;groupadd oinstall -g 1300;groupadd oper -g 1301;useradd oracle -u 1000 -g...
hungryminds.bea.weblogic.server.bible.pdf WebLogic宝典
WEBLOGIC 11G密码忘记,重新初始化密码方法! WEBLOGIC 11G密码忘记,重新初始化密码方法! WEBLOGIC 11G密码忘记,重新初始化密码方法!
weblogic11G创建集群,可视化配置,图文详解,步骤清晰到位,保姆级教程
NULL 博文链接:https://tianshidehuhuan.iteye.com/blog/1157705
weblogic补丁2020.4.15号发布.zip https://www.oracle.com/security-alerts/cpuapr2020.html 包含了:p30857748_1036_Generic.zip p30965714_122130_Generic.zip p30970477_122140_Generic.zip
开发部署中间件weblogic下载,经实践试用,没有任何问题。 WebLogic是美国Oracle公司出品的一个application server,确切的说是一个基于JAVAEE架构的中间件,WebLogic是用于开发、集成、部署和管理大型分布式Web...
Weblogic教程,PDF格式... Weblogic教程,PDF格式...
标签:ant-weblogic-1.6.jar.zip,ant,weblogic,1.6,jar.zip包下载,依赖包
WebLogic长期以来一直被认为是市场上最好的J2EE工具之一,本文档主要介绍如何在Weblogic中部署 jks 安全证书