溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

           

          Struts2文件上傳一些小細節

          大家都知道struts2對文件上傳做了很好的封裝,使文件上傳不再那么恐怖。這里面struts2的文件上傳主要依賴的是org.apache.struts2.interceptor.FileUploadInterceptor這個攔截器

          關于這個攔截器我不想做過多的研究,這里主要討論的是該攔截器里面定義的幾個關于文件上傳的重要屬性。

          protected Long maximumSize;   //允許上傳單個文件的大小單位為字節

          protected String allowedTypes; //允許上傳的文件類型詳見tomcatweb.xml文件

          protected Set allowedTypesSet;

          //允許上傳的文件類型Set集合詳見tomcatweb.xml文件

          allowedTypesallowedTypesSet屬性如有多個值之間用逗號隔開

          以上的屬性主要配置在struts.xml中對應文件上傳Action的攔截器中

          示列:

          <!-- 文件上傳Action -->

                   <action name="Upload" class="UploadAction">

                       <result name="success">success.jsp</result>

                       <result name="input">upload.jsp</result>

                       <interceptor-ref name="fileUpload">

          //設置允許上傳單個文件的大小單位為字節

                           <param name="maximumSize">102400</param>

          //允許上傳的文件類型詳見tomcatweb.xml文件

                         <param name="allowedTypesSet">image/jpeg</param>

                       </interceptor-ref>

                       <interceptor-ref name="defaultStack"/>

                   </action>

          注意:要使用文件上傳功能我們必須顯實的在對應文件上傳的Action中指定<interceptor-ref name="fileUpload">這個攔截器及在該攔截器中配置我們需要的參數,最后別忘了附加上<interceptor-ref name="defaultStack"/>這個默認的攔截器,這樣才能真正發揮struts2的文件上傳功能。

          示列代碼:

          Action中的示列代碼

          publicclass FileUploadAction extends ActionSupport {

              private File file;

              private String fileContentType;

              private String fileFileName;

              private String memo;

              @Override

              public String execute() throws Exception {

                  String path=ServletActionContext.getRequest().getRealPath("/upload");

                  if(file==null)

                  {

                      this.addFieldError("file", "文件不能為空,請選擇");

                      returnINPUT;

                  }else

                  {

                      InputStream is=new FileInputStream(this.getFile());

                      OutputStream os=new FileOutputStream(new File(path,this.getFileFileName()));

                      byte[] buf=newbyte[1024];

                      int length=0;

                      while((length=is.read(buf))>0)

                      {

                          os.write(buf, 0, length);

                      }

                      is.close();

                      os.close();

                  }

                  returnSUCCESS;

              }

              public File getFile() {

                  returnfile;

              }

             

              publicvoid setFile(File file) {

                  this.file = file;

              }

             

              public String getFileContentType() {

                  returnfileContentType;

              }

              public String getFileFileName() {

                  returnfileFileName;

              }

              public String getMemo() {

                  returnmemo;

              }

             

              publicvoid setFileContentType(String fileContentType) {

                  this.fileContentType = fileContentType;

              }

              publicvoid setFileFileName(String fileFileName) {

                  this.fileFileName = fileFileName;

              }

              publicvoid setMemo(String memo) {

                  this.memo = memo;

              }

             

          }

          jsp中的示列代碼

          <s:form action="Upload" enctype="multipart/form-data" method="post">

              <s:file name="file"></s:file>

              <s:textfield name="memo"></s:textfield>

              <s:submit></s:submit>

          </s:form>

          struts.xml中的示列代碼

          <action name="Upload" class="UploadAction">

                       <result name="success">/success.jsp</result>

                       <result name="input">/upload.jsp</result>

                       <interceptor-ref name="fileUpload">

                           <param name="maximumSize">102400</param>

                           <param name="allowedTypes">application/msword</param>

                       </interceptor-ref>

                       <interceptor-ref name="defaultStack"/>

          </action>

          注意:

          a)       設置文件上傳屬性在Action中對應的類型的java.io.File;

          b)       設置文件上傳表單的enctype="multipart/form-data" method="post"

          private File file;

          private String fileContentType;

          private String fileFileName;

          c)       紅色標注的與文件上傳表單中文件上傳屬性的name一致

          d)       藍色的為固定寫法

          e)       對應Action中攔截器的配置

          <interceptor-ref name="fileUpload">

                           <param name="maximumSize">102400</param>

                           <param name="allowedTypes">application/msword</param>

          </interceptor-ref>

          <interceptor-ref name="defaultStack"/>

          posted on 2009-10-21 10:33 雪山飛鵠 閱讀(604) 評論(0)  編輯  收藏 所屬分類: struts2
          主站蜘蛛池模板: 岗巴县| 六枝特区| 江孜县| 黄山市| 高安市| 佳木斯市| 临洮县| 高清| 布尔津县| 济阳县| 迭部县| 英山县| 越西县| 辉县市| 东兰县| 嘉鱼县| 呼伦贝尔市| 潜江市| 通榆县| 白山市| 辉南县| 息烽县| 姜堰市| 枣强县| 报价| 襄城县| 东兰县| 武邑县| 正阳县| 泰兴市| 南溪县| 肃北| 广饶县| 新丰县| 霍州市| 临湘市| 天门市| 邵阳县| 宣恩县| 永春县| 普兰店市|