weblogic与tomcat服务器之间路径的问题

现在项目做的差不多了,有时间可以整理下问题,这里提到一个weblogic与tomcat服务器之间路径的问题,刚开始用的是tomcat开发,在第一次部署的时候遇到读取文件路径的问题,经理说以前遇到过好多次,解决方法就是直接写死了服务器的路径,然后找了一下获取工程路径的代码


package cn.cntomorrow.kxgk.util;

import java.io.File;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import cn.cntomorrow.kxgk.base.ActionBase;

public class GetPath{
	private HttpServletRequest request;
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	/** 
	 * 通过上下文来取工程路径 
	 *  
	 * @return 
	 * @throws Exception 
	 */  
	public String getAbsolutePathByContext() throws Exception {  
	    String webPath = request.getSession().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 
	 */  
	public String getAbsolutePathByClass() throws Exception {  
	    String webPath = 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;  
	}  
	public String getAbsolutePathByResource() throws Exception {  
	    URL url = request.getSession().getServletContext().getResource("/");  
	    String path = new File(url.toURI()).getAbsolutePath();  
	          if (!path.endsWith("\\") && !path.endsWith("/")) {  
	        path += File.separator;  
	    }  
	    return path;  
	}  
	  
	public String init() {  
	    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) {  
	        }  
	    }  
	  
	    return webPath;  
	}  
}

还有个问题是测试环境一定要相同,就是说weblogic的版本要相同,在做测试的时候出现的问题很不像版本的问题,可是查到最后查不出来换了个和服务器上版本相同的就好了,以后每次在部署到服务器上时,出现问题了先看版本,第一次部署经验不足,出现了数据源不一致的问题,本地是tomcat的数据源,而weblogic控制台部署启动时访问报错,表现为空指针,如果服务器是weblogic,本地不是的话,平时开发一定要考虑这个路径的问题




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章