arm-linux

http://armboard.taobao.com/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  
                                  Java路径问题最终解决方案使用演示
 
 
前言
本文中,我给大家提供了一个在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工作流定义文件
 *并且把已上传的文件copyWeb应用程序根目录/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》一文中作了详细的解释。
 
 
 
 
 
 
posted on 2006-12-03 14:32  arm-linux  阅读(280)  评论(0编辑  收藏  举报