一樣的男人,不一樣的思想,造就不一樣的人生

          心胸有多大,舞臺就有多大

           

          JAVA上傳文件

          轉載至翰血寶碼 http://lecky.tianyablog.com
          commons fileupload 是Apache commons項目的一部分,FileUpload 使你很容易在servlet及web 應用中提供一個魯棒的、高性能的文件上特性。FileUpload按照RFC 1867 ( "Form-based File Upload in HTML")處理HTTP請求。即,如果HTTP request 以 POST方法提交,并且content type 設置為"multipart/form-data",那么FileUpload可以處理該請求,在web應用中提供文件上載的功能。其使用方法見commons fileupload的相關文檔。
          
           在把FileUpload與struts結合(jsp + uploadactiono)使用過程中發現,如果在action mapping配置中不指定formbean,文件上傳過程正常。如果指定了formbean,文件上傳不正常,取不到文件。以下是幾個文件片斷:
          
          upload.jsp
          
          **form action="uploadaction.do?method=uploadByFileUpload" method="post" enctype="multipart/form-data" ** **input type="file" name="uploadfile"** **/form**UploadAction.java public ActionForward uploadByFileUpload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ......
           String dir =
           request.getSession().getServletContext().getRealPath(
           "/");
           DiskFileUpload fu= new DiskFileUpload();
          
           fu.setSizeMax( UPLOAD_MAXSIZE);
           fu.setSizeThreshold( MAX_DATA_IN_MEM);
           fu.setRepositoryPath( System.getProperty("java.io.tmpdir"));
          
           try {
           List fileItem= fu.parseRequest( request);
           Iterator it= fileItem.iterator();
           while( it.hasNext()){
           FileItem item= (FileItem)it.next();
           if( !item.isFormField() && null!= item.getName() &&
           0!= item.getName().trim().length()){
           String clientPath = item.getName();
           String fileName = new File(clientPath).getName();
          
           File destDir = new File( dir);
          
           File file = new File(destDir, fileName);
           item.write( file);
           map.put( item.getFieldName(),
           dir+ File.separator+ fileName);
          
          
           }
           }
           } catch (Exception e) {
           String str= "文件上載異常,錯誤信息:"+ e.getMessage();
           System.out.println(str);
           throw new Exception( str, e);
           }
          
           ......
           }
          
          struts-config.xml
          
           name="TestForm"
           type="UploadAction"
           parameter="method" >
          
          
          
          現象:在struts-config.xml文件中,如果指定了formbean——name="TestForm" ,則文件無法正確上傳,UploadAction中的fu.parseRequest( request)方法返回值為null;如果去掉了說明formbean的name屬性,則文件可以正常上傳。
          
          原因:struts的RequestProccessor.process已經包含了處理文件上傳的方法。如果在action配置中設置了formbean ,那么在你自己的action處理request之前,struts已經在RequestProccessor.populate方法中處理了request,因此,在自己的action中就取不到上傳的文件了。
          
          處理:如果要自己在action中處理文件上傳工作,那么就不要在配置文件中配置formbean。
          
          其他選擇:如果仍需使用formbean,那么可以使用struts內置的文件上傳功能。具體使用方法見struts的相關文檔,以及struts的upload例子。以下是幾個文件片斷:
          
          upload.jsp
          基本同上,修改form標簽的action屬性
          
          
          UploadAction.java
           public ActionForward uploadByStruts(ActionMapping mapping,
           ActionForm form,
           HttpServletRequest request,
           HttpServletResponse response)
           throws Exception {
           ActionErrors errs= new ActionErrors();
          
           if (form != null){
           DynaActionForm theForm = (DynaActionForm)form;
           FormFile file = (FormFile)theForm.get("uploadfile");
           try{
           String fileName= file.getFileName();
           if ("".equals(fileName)) {return null;}
           InputStream stream = file.getInputStream();
          
           String dir =
           request.getSession().getServletContext().getRealPath(
           "/");
           OutputStream bos = new FileOutputStream(dir+"/"+fileName);
           int bytesRead = 0;
           byte[] buffer = new byte[8192];
           while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
           bos.write(buffer, 0, bytesRead);
           }
           bos.close();
           stream.close();
           }catch (FileNotFoundException fnfe) {
           ...
           }catch (IOException ioe) {
           ...
           }catch (NullPointerException e){
           ...
           }
          
           }else{
           ...
           }
          
           if (!errs.isEmpty()){
           saveErrors( request, errs);
           }
          
           return mapping.findForward( "success");
          
           }
          
          
          struts-config.xml
          
          **form-bean name="TestForm" type="org.apache.struts.action.DynaActionForm"**
           form-property name="uploadfile" type="org.apache.struts.upload.FormFile" /**
           /form-bean**
          
           **action path="/uploadaction"
           name="TestForm" **!--指定formbean--**
           type="UploadAction"
           parameter="method" **
           **forward name="success" path="/success.jsp" /**
           **/action**
          
           **controller maxFileSize="2M" /**
          
          注意,使用struts自帶的文件上傳功能,最帶文件尺寸限制用來配置。另為struts對文件上載功能提供了兩種處理實現:org.apache.struts.upload.CommonsMultipartRequestHandler 和 org.apache.struts.upload.DiskMultipartRequestHandler。struts默認的是前者,如果要使用后者,需在中配置,配置樣例如下。而且,DiskMultipartRequestHandler是使用commons uploadload實現的。

          posted on 2006-01-18 21:19 大夯 閱讀(1419) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 罗田县| 马尔康县| 巴马| 滦南县| 攀枝花市| 盈江县| 韶山市| 大埔区| 东乌珠穆沁旗| 会昌县| 兴山县| 清涧县| 叶城县| 神池县| 沙田区| 托克逊县| 方山县| 罗山县| 庆安县| 于田县| 永宁县| 资兴市| 松江区| 景洪市| 休宁县| 轮台县| 安乡县| 温泉县| 枞阳县| 哈巴河县| 商丘市| 工布江达县| 保康县| 体育| 固镇县| 合山市| 定西市| 抚顺县| 汨罗市| 江陵县| 唐河县|