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

          心胸有多大,舞臺(tái)就有多大

           

          【轉(zhuǎn)載】JAVA上傳文件

          轉(zhuǎn)載翰血寶碼 http://lecky.tianyablog.com
          commons fileupload 是Apache commons項(xiàng)目的一部分,F(xiàn)ileUpload 使你很容易在servlet及web 應(yīng)用中提供一個(gè)魯棒的、高性能的文件上特性。FileUpload按照RFC 1867 ( "Form-based File Upload in HTML")處理HTTP請(qǐng)求。即,如果HTTP request 以 POST方法提交,并且content type 設(shè)置為"multipart/form-data",那么FileUpload可以處理該請(qǐng)求,在web應(yīng)用中提供文件上載的功能。其使用方法見commons fileupload的相關(guān)文檔。
          
           在把FileUpload與struts結(jié)合(jsp + uploadactiono)使用過程中發(fā)現(xiàn),如果在action mapping配置中不指定formbean,文件上傳過程正常。如果指定了formbean,文件上傳不正常,取不到文件。以下是幾個(gè)文件片斷:
          
          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= "文件上載異常,錯(cuò)誤信息:"+ e.getMessage();
           System.out.println(str);
           throw new Exception( str, e);
           }
          
           ......
           }
          
          struts-config.xml
          
           name="TestForm"
           type="UploadAction"
           parameter="method" >
          
          
          
          現(xiàn)象:在struts-config.xml文件中,如果指定了formbean——name="TestForm" ,則文件無法正確上傳,UploadAction中的fu.parseRequest( request)方法返回值為null;如果去掉了說明formbean的name屬性,則文件可以正常上傳。
          
          原因:struts的RequestProccessor.process已經(jīng)包含了處理文件上傳的方法。如果在action配置中設(shè)置了formbean ,那么在你自己的action處理request之前,struts已經(jīng)在RequestProccessor.populate方法中處理了request,因此,在自己的action中就取不到上傳的文件了。
          
          處理:如果要自己在action中處理文件上傳工作,那么就不要在配置文件中配置formbean。
          
          其他選擇:如果仍需使用formbean,那么可以使用struts內(nèi)置的文件上傳功能。具體使用方法見struts的相關(guān)文檔,以及struts的upload例子。以下是幾個(gè)文件片斷:
          
          upload.jsp
          基本同上,修改form標(biāo)簽的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對(duì)文件上載功能提供了兩種處理實(shí)現(xiàn):org.apache.struts.upload.CommonsMultipartRequestHandler 和 org.apache.struts.upload.DiskMultipartRequestHandler。struts默認(rèn)的是前者,如果要使用后者,需在中配置,配置樣例如下。而且,DiskMultipartRequestHandler是使用commons uploadload實(shí)現(xiàn)的。

          posted on 2006-01-18 21:15 大夯 閱讀(152) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 铜陵市| 张家界市| 上杭县| 泽库县| 定安县| 沛县| 永福县| 屯留县| 海口市| 东港市| 奉新县| 石渠县| 大埔区| 克拉玛依市| 大兴区| 达拉特旗| 望奎县| 和龙市| 宜昌市| 年辖:市辖区| 锡林郭勒盟| 成武县| 东阿县| 卢氏县| 封丘县| 图们市| 江川县| 鱼台县| 车致| 怀化市| 肥西县| 屯门区| 唐河县| 青海省| 黎川县| 三门峡市| 尼勒克县| 东辽县| 临邑县| 镇平县| 汶川县|