Greedy的工作室

          posts - 23,comments - 7,trackbacks - 0
          本文用的是Struts1.1的org.apache.struts.upload.FormFile類。很方便,不用自己寫。也不用寫一個jsp調用jspsmartupload就可以搞定。

          FormFile的幾個方法:
          String getContentType();
          byte[] getFileData();
          String getFileName();
          int getFileSize();
          如果有必要可以在validate里調用一下做一些判斷。

          選擇上傳文件頁面:selfile.jsp

          --------------------------------------------------------------------------------
          <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
          <html:html>
          <html:form action="/uploadsAction.do" enctype="multipart/form-data">
          <html:file property="theFile"/>
          <html:submit/>
          </html:form>
          </html:html>

          --------------------------------------------------------------------------------
          UpLoadAction.java
          --------------------------------------------------------------------------------

          import java.io.*;
          import javax.servlet.http.*;
          import org.apache.struts.action.*;
          import org.apache.struts.upload.FormFile;

          public class UpLoadAction extends Action {
           public ActionForward execute(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response)
               throws Exception {
             if (form instanceof uploadsForm) {//如果form是uploadsForm

                 UpLoadForm theForm = (UpLoadForm ) form;
                 FormFile file = theForm.getTheFile();//取得上傳的文件
                 try {
                   InputStream stream = file.getInputStream();//把文件讀入
                   String filePath = request.getRealPath("/");//取當前系統路徑
                   OutputStream bos = new FileOutputStream(filePath + "/" +
                                                           file.getFileName());//建立一個上傳文件的輸出流
                   //System.out.println(filePath+"/"+file.getFileName());
                   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(Exception e){
                   System.err.print(e);
                 }
                 //request.setAttribute("dat",file.getFileName());
                 return mapping.findForward("display");
             }
             return null;
           }
          }


          --------------------------------------------------------------------------------
          UpLoadForm.java
          --------------------------------------------------------------------------------

          import javax.servlet.http.HttpServletRequest;
          import org.apache.struts.action.*;
          import org.apache.struts.upload.*;

          public class UpLoadForm extends ActionForm {
           public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
           protected FormFile theFile;
           public FormFile getTheFile() {
               return theFile;
           }
           public void setTheFile(FormFile theFile) {
               this.theFile = theFile;
           }
           public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
           {
               ActionErrors errors = null;
               //has the maximum length been exceeded?
               Boolean maxLengthExceeded = (Boolean)
                       request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
               if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
               {
                   errors = new ActionErrors();
                   errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
               }
               return errors;

           }
          }
          //這是相對應的form,還有其他屬性可以設置,具體可以參考struts的上傳例子。


          --------------------------------------------------------------------------------
          struts-config.xml
          --------------------------------------------------------------------------------

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
          <struts-config>
           <form-beans>
             <form-bean name="uploadsForm" type="UpLoadForm" />
           </form-beans>
           <action-mappings>
             <action name="uploadsForm" type="UpLoadAction" path="/uploadsAction">
               <forward name="display" path="/display.jsp" />
             </action>
           </action-mappings>
          </struts-config>
          <!--display.jsp就是隨便寫一個成功頁-->
          posted on 2008-06-27 10:47 greedy 閱讀(189) 評論(0)  編輯  收藏 所屬分類: Struts

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


          網站導航:
           
          主站蜘蛛池模板: 星子县| 太湖县| 萝北县| 蒙山县| 枣庄市| 大埔县| 崇文区| 洛阳市| 合山市| 酒泉市| 固阳县| 乌审旗| 枣强县| 资讯 | 宁化县| 东山县| 岳阳县| 廉江市| 柘城县| 平邑县| 安吉县| 油尖旺区| 叶城县| 筠连县| 平和县| 南开区| 海丰县| 弋阳县| 温宿县| 三明市| 祥云县| 平遥县| 大关县| 长治县| 宣城市| 鄂州市| 茶陵县| 陇南市| 普洱| 丰镇市| 高淳县|