Terry.Li-彬

          虛其心,可解天下之問(wèn);專(zhuān)其心,可治天下之學(xué);靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評(píng)論 :: 0 Trackbacks
          前言
          在《Java路徑問(wèn)題最終解決方案可定位所有資源的相對(duì)路徑尋址》一文中,我給大家提供了一個(gè)助手類(lèi)ClassLoaderUtil ,和它的public static URL getExtendResource(String relativePath)方法。這個(gè)方法能夠接受“../”這樣的參數(shù),允許我們用相對(duì)路徑來(lái)定位classpath外面的資源。這樣,我們就可以使用相對(duì)于classpath的路徑,定位所有位置的資源!
          本文中,我給大家提供了一個(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工作流定義文件
           *并且把已上傳的文件copyWeb應(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ì)的解釋。
           
          posted on 2007-09-06 17:55 禮物 閱讀(269) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java
          主站蜘蛛池模板: 宜昌市| 宝兴县| 平江县| 简阳市| 九江市| 临邑县| 永年县| 孙吴县| 明溪县| 东乡县| 荣成市| 太仓市| 宿州市| 大庆市| 无极县| 开阳县| 宾阳县| 万安县| 繁峙县| 射洪县| 济源市| 斗六市| 新田县| 普洱| 博白县| 房山区| 建始县| 白水县| 积石山| 锦州市| 永康市| 东兰县| 中江县| 皋兰县| 沧州市| 西乌珠穆沁旗| 噶尔县| 嘉峪关市| 九寨沟县| 汉沽区| 和硕县|