本文引自:
http://fjy26.itpub.net/war包中的文件的讀取
在開發J2EE Web應用時,在開發階段通常采用目錄的部署方式,而在正式運行時通常把web應用打包為單個的.war文件進行方便地部署。也就是在你的應用目錄(比如WebLogic的DefaultWebApp)下,執行下面的命令:
這樣,要部署到正式系統時就非常方便,只需要把這個.war文件拷貝到WebLogic的applications目錄或Tomcat的webapps目錄下即可自動進行部署。Tomcat會對部署的.war應用包進行自動監控、解包,所以不會出現下面提到的問題。而WebLogic并不會自動解包.war,所以如果在你的應用中,需要讀取原來應用中的配置文件或其它資源文件時,就會發現,在解包部署時,正常運行的程序,在WebLogic中打包部署時,運行卻出錯,會報告找不到該文件。例如下面的應用:
[pre] |--DefaultWebApp
|--index.jsp
|--.....jsp
|--WEB-INF
|-- web.xml
|-- log4j.properties
|-- classes
......[/pre]
其中使用到了Log4J作為日志輸出工具,Log4J的配置文件log4j.propertes放在DefaultWebAppWEB-INF目錄下。Log4J通過一個自動加載的Servlet進行初始化,初始化代碼如下:
- ServletContext context = getServletContext();
- org.apache.log4j.PropertyConfigurator.configure(context.getRealPath("/") +
- "/WEB-INF/log4j.properties");
其中,context.getRealPath("/")得到當前Web應用的真實根目錄,比如,如果你的WebLogic安裝在D:bea下,在Windows下context.getRealPath("/")通常會返回:
D:beawlserver6.1configmydomainapplicationsDefaultWebApp
在UNIX下類似:
/bea/wlserver6.1/config/mydomain/applications/DefaultWebApp
這樣,和"/ WEB-INF /log4j.properties"拼接后,就得到了log4j.properties文件的真實路徑,Log4J通過文件IO讀取這個配置文件,完成初始化。
現在一切正常!測試通過后,將DefaultWebApp下的所有文件打為一個.war包,進行部署時,發現系統報告找不到“D:beawlserver6.1null WEB-INF log4j.properties”文件!如果你的應用中還需要讀取其它已經被打包到war包中的文件,都會報告找不到文件。并且,系統并不會到D:beawlserver6.1configmydomainapplicationsDefaultWebApp目錄下尋找,而會到D:beawlserver6.1null下尋找。這是因為context.getRealPath("/")返回了null。查看ServletContext的API文檔,
public String getRealPath(String path)
……
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason
(such as when the content is being made available from a .war archive).
原來,對一個打包的應用來說,是沒有RealPath的概念的,調用getRealPath只會簡單地返回null。其實,也很好理解,一個文件被打包入了.war文件,就不存在目錄結構了(雖然包中仍然存在目錄結構,但這不等同于文件系統中的目錄結構)。所以,對war包中的資源是無法得到RealPath的。這樣也就無從通過文件IO進行讀取了。那么,如何讀取war包中的資源呢?答案是使用ServletContext.getResourceAsStream(String)方法。
對于org.apache.log4j.PropertyConfigurator,有如下幾種配置方法:
- staticvoid configure(Properties properties);
- staticvoid configure(String configFilename);
- staticvoid configure(URL configURL);
既然,現在不能得到war包中的Log4J的配置文件,那么可以通過讀入InputStream,構造一個Properties,通過configure(Properties properties)方法同樣可以完成配置。示例代碼如下:
- InputStream is = getServletContext().
- getResourceAsStream("/WEB-INF/log4j.properties");
- Properties props = newProperties();
- try {
- props.load(is);
- } catch (IOException e) {
- System.err.println("Load log4j configuration failed");
- }
- PropertyConfigurator.configure(props);
那么,現在對于war應用可以成功運行,但如果現在不通過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.可見,通過getResourceAsStream可以獲取包括本地文件系統、遠程文件系統、war包等資源。不會出現上面擔心的問題。
結論:在開發J2EE Web應用時,如果需要讀取本應用中的文件,盡量使用ServletContext.getResourceAsStream進行,而不要使用文件IO。