Java學(xué)習(xí)

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經(jīng)搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進(jìn)步

           

          JSF實(shí)現(xiàn)文件上傳

          JSF實(shí)現(xiàn)文件上傳
          2007-03-13 22:31

            目前 JSF 對于檔案上傳的支持很差,JSC 內(nèi)更是缺少檔案上傳的組件
          除非使用 Orcale ADF 的組件,但是 Oracle ADF 并非免費(fèi)的。
          本篇文章主要就是跟大家分享如何在 JSF 內(nèi)使用免費(fèi)的 Jakarta Commons FileUpload (http://jakarta.apache.org/commons/fileupload/ ) 組件來實(shí)作檔案上傳功能。
          有幾點(diǎn)注意事項(xiàng),這邊必須先要提出的:
          1. Servlet 部分改自本討論區(qū) JiaYun 大大的「檔案上傳 JSP 小范例」,希望他不會介意!^^" (如果需要更清楚的 Jakarta Commons FileUpload 范例,那篇文章

          會是更好的選擇。)
          2. 檔案上傳,必須把 form 的 enctype 更改如下:

          <h:form binding="#{AttachFile.form1}" enctype="multipart/form-data" id="form1">


          但是以上的 JSF 標(biāo)簽 compile 都會正常,但是在這個(gè) form 里面,其它組件的 action 都會無法正常觸發(fā)。所以這邊僅使用一般的 JSP 頁面。
          3. 這篇獻(xiàn)丑的教學(xué)其實(shí)是使用 JSF(其它頁面) -> JSP(選擇檔案頁面) -> Servlet(儲存使用者上傳的檔案) -> JSF(其它頁面) 的流程。
          說穿了,只是使用舊的 JSP + Servlet 技術(shù),請各位高手海涵。

           流程一(選擇檔案的 JSP 頁面):

          <f:verbatim>
               <form action="UploadFile" enctype="multipart/form-data" method="post">
                 File:<input name="txtFile" type="file"/><br/>                        
                 Description:<input name="txtDesc" type="text"/><br/>
                 <input type="checkbox" name="chkAdd"/>Add another file<br/>
                 <input style="height: 27px; left: 0px; top: 96px; position: absolute; width: 72px" type="submit" value="Add"/>
                 <input style="height: 27px; left: 96px; top: 96px; position: absolute; width: 72px" type="submit" value="Cancel"/>
               </form>
          </f:verbatim>                
          <h:form binding="#{AttachFile.form1}" id="form1"/>

           1. 這個(gè)頁面包含:
          一個(gè) type="file" 的 textbox
          一個(gè)一般的 textbox 提供為輸入檔案描述
          一個(gè) checkbox,提供多重上傳檔案
          一個(gè) Add 及一個(gè) Cancel 的 Button
          2. 如果使用者選取 checkBox,在后面的 Servlet 處理完,則回到這一頁 ,選擇另一個(gè)上傳的檔案。 如果沒有勾選,則前進(jìn)到其它的 JSF 頁面。
          3. UploadFile 為后面要處理檔案儲存的 Servlet 名稱。
          4. 以上只寫出修改的地方,其它 JSF 預(yù)設(shè)的 tag 就不寫出來了。

           流程二(web.xml):
          <servlet>
               <servlet-name>Upload_File</servlet-name>
               <servlet-class>webapplication1.UploadFile</servlet-class>
          </servlet>
          <servlet-mapping>
               <servlet-name>Upload_File</servlet-name>
               <url-pattern>/UploadFile</url-pattern>
          </servlet-mapping>
          1. 請把這一段程序代碼與 web.xml 內(nèi)的其它 servlet tag 放在一塊,比較不會出問題。
          2. 這段大致的意義為:當(dāng)你向 server 要求 /UploadFile 這個(gè) URL 時(shí), server 會使用 webapplication1.UploadFile 這個(gè) servlet 來處理你的要求。

           流程三(后端處理檔案的 Servlet):
          UploadFile
          import java.io.*;
          import java.util.*;
          import java.sql.*;
          import javax.servlet.*;
          import javax.servlet.http.*;
          import org.apache.commons.fileupload.*;

          public class UploadFile extends HttpServlet{
                 public void doPost(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
                   //取得 JSF 的 SessionBean
                   SessionBean1 sesBean=(SessionBean1)servletRequest.getSession().getAttribute("SessionBean1");
                   //取得 JSF 的 ApplicationBean
                   ApplicationBean1 appBean=(ApplicationBean1)getServletContext().getAttribute("ApplicationBean1");
                   // 宣告將上傳之檔案放置到服務(wù)器的 / .... /upload 目錄中
                   //String saveDirectory = appBean.getAttachPath();
                   // 宣告暫存目錄
                   //String tmpDirectory = "c:\\";
                   // 宣告限制上傳之檔案總大小為, 單位為 byte, -1 表示無限制
                   int maxPostSize = -1;
                   // 宣告儲存敘述上傳檔案內(nèi)容的變量
                   String FileDescription = "";
                   // 宣告是否接下來要繼續(xù)新增檔案
                   boolean hasMore = false;
                   // 宣告儲存上傳文件名稱的變量
                   String FileName = "";
                   // 宣告儲存上傳檔案大小的變量
                   long FileSize = 0;
                   // 宣告儲存上傳檔案型態(tài)的變量
                   //String ContentType = null;
                   // 計(jì)算上傳檔案之個(gè)數(shù)
                   int count = 0 ;

                   try {
                     DiskFileUpload upload = new DiskFileUpload();
                     // 處理中文檔名問題
                     upload.setHeaderEncoding("UTF-8");
                     // 設(shè)定內(nèi)存存放數(shù)據(jù)的大小, 超過則寫入檔案, 有設(shè)定暫存目錄, 暫存盤置于暫存目錄下
                     //upload.setSizeThreshold(4096);
                     // 設(shè)定總上傳大小限制
                     //upload.setSizeMax(maxPostSize);
                     // 設(shè)定暫存目錄
                     //upload.setRepositoryPath(tmpDirectory);
                     List items =upload.parseRequest((HttpServletRequest)servletRequest);
                     Iterator iter = items.iterator();
                     FileItem fItem=null;
                  
                     while(iter.hasNext()) {
                             FileItem item = (FileItem) iter.next();
                             if (item.isFormField()) {
                                 // 一般字段
                                 if(item.getFieldName().equalsIgnoreCase("txtDesc")) {
                                     // 取得檔案敘述
                                     FileDescription = item.getString("UTF-8");
                                 }
                                 else if(item.getFieldName().equalsIgnoreCase("chkAdd")) {
                                     // 取得接下來將前往的頁面
                                     hasMore = item.getString().equalsIgnoreCase("on") ? true: false;
                                 }
                             }
                             else {
                                 // 檔案字段
                                 // 否則取得檔案信息
                                 FileName = item.getName();
                                 // 因?yàn)椴煌臑g覽器會造成傳遞 path + filename, 有些則只有 filename
                                 // for wintel platform
                                 FileName = FileName.substring(FileName.lastIndexOf("\\")+1);
                                 // for unix-like platform
                                 FileName = FileName.substring(FileName.lastIndexOf("/")+1);
                              
                                 //ContentType = item.getContentType();
                                 FileSize = item.getSize();                    
                              
                                 fItem=item;
                             }
                         }

                         //--Delete--
                         //如果需要的話
                         //你可以在這里使用前面取得的 SessionBean 及 ApplicationBean
                         //里面的參數(shù)對你的數(shù)據(jù)庫做操作,并把檔案描述在這里存入數(shù)據(jù)庫
                      
                         //寫入檔案
                         File uploadedFile = new File(appBean.getAttachPath() + FileName);
                         fItem.write(uploadedFile);            
                     }
                     catch(Exception ex) {
                       //例外處理            
                     }
                     //導(dǎo)向不同的頁面
                     finally {
                         if(hasMore)
                             servletResponse.sendRedirect("faces/AttachFile.jsp");
                         else
                             servletResponse.sendRedirect("faces/Schedule.jsp");
                     }
                 }
          }

          1. 前面的取得 SessionBean1 及 ApplicationBean1,看個(gè)人需要,如果你需要取得 JSF 內(nèi)的 SessionBean 及 ApplicationBean,可以參考這個(gè)用法。
          2. 最后的 servletResponse.sendRedirect 會根據(jù)前面的 JSP 頁面,是否有勾選 chkAdd 來決定是要回到前面的 JSP 頁面來繼續(xù)新增檔案,或是前往其它 JSF 頁面。
          3. 請注意 faces/Schedule.jsp 前面的 faces ,如果不加可能可會出現(xiàn) Cannot find FacesContext 的錯誤。

          posted on 2008-10-24 10:16 找個(gè)美女做老婆 閱讀(3181) 評論(0)  編輯  收藏


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


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

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 古田县| 大厂| 潼南县| 长宁区| 扬中市| 大英县| 启东市| 资阳市| 贺兰县| 保亭| 柯坪县| 罗城| 油尖旺区| 舒兰市| 咸宁市| 南乐县| 商水县| 孙吴县| 大城县| 晋城| 改则县| 广元市| 赤城县| 敦煌市| 肥城市| 宽甸| 东乡县| 永登县| 宝清县| 崇州市| 木兰县| 安顺市| 安平县| 大悟县| 桐梓县| 葵青区| 迁安市| 邯郸市| 山东| 朝阳区| 莫力|