前言
本文中,我給大家提供了一個(gè)在JavaEE程序中使用這個(gè)便利方法尋找相對(duì)路徑的代碼實(shí)例。
在《JavaEE路徑陷阱之getRealPath》一文中,探討了JavaEE程序中資源尋址的問(wèn)題,有興趣的讀者可以看看那篇文章。
Java路徑問(wèn)題最終解決方案使用演示
示例背景
使用ClassLoaderUtil.getExtendResource()方法進(jìn)行尋址的這個(gè)示例,是一個(gè)JavaEE程序,使用了SpringMVC框架進(jìn)行前臺(tái)開(kāi)發(fā)。上傳文件部分,使用了Apache的commons upload技術(shù)。
這個(gè)模塊的功能是,向服務(wù)器上傳一個(gè)JBoss的工作流引擎Jbpm的工作流定義文件。然后把它部署到服務(wù)器上。同時(shí),把上傳的工作流定義文件保存到服務(wù)器的 Web應(yīng)用程序根目錄/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
*這個(gè)類(lèi)負(fù)責(zé)上傳并部署Jbpm工作流定義文件
*并且把已上傳的文件copy到Web應(yīng)用程序根目錄/WEB-INF/jbpm/upload/目錄下,以備查閱!
*
*/
publicclass UploadAndDeployJbpmProcessDefinition extends BaseController {
/**
*Service,部署本地上傳的xml業(yè)務(wù)程序定義文件到服務(wù)器端的數(shù)據(jù)庫(kù)!
*本Bean是單例。 運(yùn)行時(shí),不set這個(gè)變量。只在初始化載入Spring容器時(shí)調(diào)用set方法。注意同步資源!
*/
private IDeployProcessDefinition deployProcessDefinition;
/**
*這個(gè)方法,直接返回上傳、部署工作流定義頁(yè)面。這是為了用.page控制上傳頁(yè)面的訪問(wèn)權(quán)。
*@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("上傳文件出錯(cuò)!未能成功上傳文件!");
}else{
//部署上傳的文件
this.getDeployProcessDefinition().deployProcessDefinitionTransaction(file.getInputStream());
File destFile=null;
/**
*使用自定義的方法,實(shí)現(xiàn)了相對(duì)于classpath的相對(duì)路徑尋址。
*/
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;
}
}
后記
這里,我使用了自己實(shí)現(xiàn)的ClassLoaderUtil.getExtendResource()方法,實(shí)現(xiàn)了相對(duì)于classpath的相對(duì)路徑尋址。
沒(méi)有使用ServletContext接口提供的尋址方法。這樣的代碼,不依賴(lài)于JavaEE環(huán)境,依賴(lài)的是標(biāo)準(zhǔn)的JavaSE,可以用在任何Java程序中!
如果你要使用ServletContext接口提供的尋址方法,那么請(qǐng)一定不要使用getRealPath(“/”)方法,而應(yīng)該使用getResource()方法或者getResourceAsStream()方法尋址。參數(shù)應(yīng)該是“/”開(kāi)頭的相對(duì)路徑,相對(duì)的是Web應(yīng)用程序根目錄的相對(duì)路徑,而不是classpath的相對(duì)路徑。具體原因,在《JavaEE路徑陷阱之getRealPath》一文中作了詳細(xì)的解釋。