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,本地不是的話,平時開發一定要考慮這個路徑的問題




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