前言
本文中,我給大家提供了一個在JavaEE程序中使用這個便利方法尋找相對路徑的代碼實例。
在《JavaEE路徑陷阱之getRealPath》一文中,探討了JavaEE程序中資源尋址的問題,有興趣的讀者可以看看那篇文章。
Java路徑問題最終解決方案使用演示
示例背景
使用ClassLoaderUtil.getExtendResource()方法進行尋址的這個示例,是一個JavaEE程序,使用了SpringMVC框架進行前臺開發。上傳文件部分,使用了Apache的commons upload技術。
這個模塊的功能是,向服務器上傳一個JBoss的工作流引擎Jbpm的工作流定義文件。然后把它部署到服務器上。同時,把上傳的工作流定義文件保存到服務器的 Web應用程序根目錄/WEB-INF/jbpm/upload/目錄下,以備查閱!
源代碼:
import java.io.File;
import java.net.URI;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.withub.common.base.BaseController;
import com.withub.common.util.ClassLoaderUtil;
import com.withub.common.util.IDeployProcessDefinition;
import com.withub.wcms.UrlMap;
import com.withub.wcms.manage.deployProcessDefinition.jbpm.bean.FileUploadBean;
/**
*@author沈東良shendl_s@hotmail.com
*Nov27,2006 1:31:25PM
*這個類負責上傳并部署Jbpm工作流定義文件
*并且把已上傳的文件copy到Web應用程序根目錄/WEB-INF/jbpm/upload/目錄下,以備查閱!
*
*/
publicclass UploadAndDeployJbpmProcessDefinition extends BaseController {
/**
*Service,部署本地上傳的xml業務程序定義文件到服務器端的數據庫!
*本Bean是單例。 運行時,不set這個變量。只在初始化載入Spring容器時調用set方法。注意同步資源!
*/
private IDeployProcessDefinition deployProcessDefinition;
/**
*這個方法,直接返回上傳、部署工作流定義頁面。這是為了用.page控制上傳頁面的訪問權。
*@paramrequest
*@paramresponse
*@return
*@throwsException
*/
public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
returnnew ModelAndView(UrlMap.map("manage.deployProcessDefinition.list"));
}
/**
*
*@paramrequest
*@paramresponse
*@paramcommand
*@return
*@throwsException
*/
public ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,FileUploadBean command) throws Exception {
// let's see if there's content there
MultipartFile file = command.getFile();
if (file == null) {
// hmm, that's strange, the user did not upload anything
thrownew RuntimeException("上傳文件出錯!未能成功上傳文件!");
}else{
//部署上傳的文件
this.getDeployProcessDefinition().deployProcessDefinitionTransaction(file.getInputStream());
File destFile=null;
/**
*使用自定義的方法,實現了相對于classpath的相對路徑尋址。
*/
String uploadPath=ClassLoaderUtil.getExtendResource("../jbpm/upload/").toString();
String uploadFile=uploadPath+String.valueOf(new Date().getTime())+"_"+file.getOriginalFilename();
destFile=new File(new URI(uploadFile));
file.transferTo(destFile);
}
// well, let's do nothing with the bean for now and return
//return super.onSubmit(request, response, command, errors);
returnnew ModelAndView(UrlMap.map("manage.deployProcessDefinition.success"));
}
/**
*@paramargs
*/
publicstaticvoid main(String[] args) {
/**
*
*/
}
/**
*@returnthedeployProcessDefinition
*/
public IDeployProcessDefinition getDeployProcessDefinition() {
returndeployProcessDefinition;
}
/**
*@paramdeployProcessDefinitionthedeployProcessDefinitiontoset
*/
publicvoid setDeployProcessDefinition(
IDeployProcessDefinition deployProcessDefinition) {
this.deployProcessDefinition = deployProcessDefinition;
}
}
后記
這里,我使用了自己實現的ClassLoaderUtil.getExtendResource()方法,實現了相對于classpath的相對路徑尋址。
沒有使用ServletContext接口提供的尋址方法。這樣的代碼,不依賴于JavaEE環境,依賴的是標準的JavaSE,可以用在任何Java程序中!
如果你要使用ServletContext接口提供的尋址方法,那么請一定不要使用getRealPath(“/”)方法,而應該使用getResource()方法或者getResourceAsStream()方法尋址。參數應該是“/”開頭的相對路徑,相對的是Web應用程序根目錄的相對路徑,而不是classpath的相對路徑。具體原因,在《JavaEE路徑陷阱之getRealPath》一文中作了詳細的解釋。