Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          前言
          在《Java路徑問題最終解決方案可定位所有資源的相對路徑尋址》一文中,我給大家提供了一個助手類ClassLoaderUtil ,和它的public static URL getExtendResource(String relativePath)方法。這個方法能夠接受“../”這樣的參數,允許我們用相對路徑來定位classpath外面的資源。這樣,我們就可以使用相對于classpath的路徑,定位所有位置的資源!
          本文中,我給大家提供了一個在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 2007-09-06 17:55 禮物 閱讀(275) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 疏附县| 自治县| 灵寿县| 溆浦县| 承德县| 万全县| 吴忠市| 基隆市| 浪卡子县| 富锦市| 来宾市| 宜黄县| 广宁县| 高邑县| 平遥县| 青海省| 东阳市| 蛟河市| 晋州市| 禄劝| 玉屏| 博乐市| 桃江县| 新泰市| 伽师县| 安仁县| 泰宁县| 康马县| 景东| 江油市| 卢氏县| 酉阳| 南乐县| 万荣县| 合山市| 大城县| 齐齐哈尔市| 扶沟县| 威信县| 三江| 邢台县|