徹底解決java WEB專案的檔案路徑問題(war包)

weixin_33981932發表於2018-03-08

在j2EE專案中,我們通常會把專案打包公佈,通常是war包,作為獨立單元,能夠遠端上傳,載入,公佈,還能夠實現版本號控制,但這與我們開發過程中使用MyEclipse自己主動部署有所不同,下面做具體說明.

使用war包部署,在tomcat和WebLogic下部署有非常大的差別,tomcat會把war自己主動加壓到當前資料夾下,然後再用解壓的資料夾來公佈,這與一般不會出現太大的意外,但假設是用WebLogic公佈,就不會被解壓,假設開發過程中不注意,war包公佈後就有可能出現找不到檔案的現象。比如:使用下面方法獲取路徑:

 

this.getClass().getClassLoader().getResource("/").getPath(); 


這與得到的是絕對Uri路徑,比如::/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/。帶碟符的路徑,顯然在war中無法依據碟符來找到檔案

 

另外一種方式:

 

this.getClass().getResource("/").getPath(); 

那麼這樣獲取行不行呢?

經試驗。這與獲取的是當前類的Uri資料夾,比如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/ 也是絕對路徑,顯然無法適用於war包。

事實上以上兩種方式都走入了岔路,由於讀取檔案未必要讀取路徑。檔案操作一般都要轉換為流的方式,既然要讀取檔案,不如直接讀成輸入流,也少了一步封裝。請看下面方式:

InputStream is= this.getClass().getResourceAsStream("/config/bctcms/" + templateFileName);

意思是讀取classes資料夾中,資料夾config/bctcms/下。檔名稱為templateFileName的檔案輸入流。經試驗在war中能夠正常讀取到。

 

該方法存在一個弊端。僅僅能讀取classes資料夾下的檔案。對於其它資料夾下的檔案無能為力,顯然並不適用於全部場景。

 

假設檔案在WEB-INF資料夾下。怎樣進行讀取呢?

答案是使用ServletContext.getResourceAsStream(String)方法。

也就是先得到上下文資訊,然後通過以project資料夾為root的絕對路徑。找到檔案,舉例說明:

 

InputStream is= context.getResourceAsStream(templatePath + "/" + templateFileName);
templatePath="/WEB-INF/classes/config/bctcms/"
templateFileName="source.xls"

能夠看到templatePath是相對於context root 的路徑,而不是相對於classes,這樣即使檔案在WEB-INF其它資料夾下。也能夠順利找到。經測試,對war的情況支援良好。

 

請來看看ServletContext.getResourceAsStream的API文件,
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root. 
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

 

相信大家都看得懂。就不用贅述了。僅僅是有個問題,context是個什麼東西?答案:ServletContext。上下文資訊,在j2EE類中使用request獲得。如:

 

ServletContext context = request.getSession().getServletContext();

那麼在普通類中怎樣獲取呢?臨時特別好的辦法。使用application是一種方式,第二種方式就是想辦法先後去request物件,如:

 

 

RequestAttributes ra = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
this.context = request.getSession().getServletContext();

這樣也是能夠獲得Context的

 

但假設是JSP中,能夠直接這麼獲取輸入流:

 

application.getResourceAsStream("xxx”);


路徑問題不要糾結太久,依據以上辦法解決,最好都用ServletContext方法來獲取。僅僅須要知道一種情況就能夠了。達到目的才是最重要的。對吧?

 

有不正確之處,歡迎大家進行補充糾錯!

update:當使用spring定時器時:request和servletContext就獲取不到了,那麼要使用這種辦法直接獲取ServletContext:

ContextLoader.getCurrentWebApplicationContext().getServletContext()

update20151228:

寫檔案的時候可能須要獲得路徑,比方上傳檔案的時候就須要輸出流。而通過ServletContext不可以直接獲得輸出流的,但可以直接獲取實際路徑。如:

 

servletContext.getRealPath("/")


這樣獲得的路徑是${context}/路徑,再依據子路徑和檔名稱獲取輸出流。

 

 

 

來源:https://www.cnblogs.com/zhchoutai/p/7124409.html

 

相關文章