前言
本文中,我给大家提供了一个在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
*
Nov
27,
2006
1:31:25
PM
*
这个类负责上传并部署
Jbpm
工作流定义文件
*
并且把已上传的文件
copy
到
Web
应用程序根目录
/WEB
-
INF/jbpm/upload/
目录下,以备查阅!
*
*/
publicclass
UploadAndDeployJbpmProcessDefinition
extends
BaseController {
/**
*
Service,
部署本地上传的
xml
业务程序定义文件到服务器端的数据库!
*
本
Bean
是单例。
运行时,不
set
这个变量。只在初始化载入
Spring
容器时调用
set
方法。注意同步资源!
*/
private
IDeployProcessDefinition
deployProcessDefinition
;
/**
*
这个方法,直接返回上传、部署工作流定义页面。这是为了用
.page
控制上传页面的访问权。
*
@param
request
*
@param
response
*
@return
*
@throws
Exception
*/
public
ModelAndView list(HttpServletRequest request,HttpServletResponse response)
throws
Exception{
returnnew
ModelAndView(UrlMap.map(
"manage.deployProcessDefinition.list"
));
}
/**
*
*
@param
request
*
@param
response
*
@param
command
*
@return
*
@throws
Exception
*/
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"));
}
/**
*
@param
args
*/
publicstaticvoid
main(String[] args) {
/**
*
*/
}
/**
*
@return
the
deployProcessDefinition
*/
public
IDeployProcessDefinition getDeployProcessDefinition() {
return
deployProcessDefinition
;
}
/**
*
@param
deployProcessDefinition
the
deployProcessDefinition
to
set
*/
publicvoid
setDeployProcessDefinition(
IDeployProcessDefinition deployProcessDefinition) {
this
.
deployProcessDefinition
= deployProcessDefinition;
}
}
后记
这里,我使用了自己实现的
ClassLoaderUtil.getExtendResource()方法,实现了
相对于classpath的相对路径寻址。
没有使用ServletContext接口提供的寻址方法。这样的代码,不依赖于JavaEE环境,依赖的是标准的JavaSE,可以用在任何Java程序中!
如果你要使用ServletContext接口提供的寻址方法,那么请一定不要使用getRealPath(“/”)方法,而应该使用getResource()方法或者getResourceAsStream()方法寻址。参数应该是“/”开头的相对路径,相对的是Web应用程序根目录的相对路径,而不是classpath的相对路径。具体原因,在《
JavaEE路径陷阱之getRealPath》一文中作了详细的解释。
本文介绍了一个用于解决Java路径问题的助手类ClassLoaderUtil及其方法getExtendResource(StringrelativePath),并通过一个JavaEE程序实例展示了如何使用该方法进行资源寻址。此示例涉及上传文件至服务器,并将文件保存至特定目录,以供查阅。

被折叠的 条评论
为什么被折叠?



