最愛Java

          書山有路勤為徑,學(xué)海無涯苦作舟

          Struts2學(xué)習(xí)筆記 —— 上傳和下載

              Struts2默認(rèn)使用Jakarta的Common-FileUpload的文件上傳解析器。見struts.properties配置文件中:

          #指定使用COS的文件上傳解析器
          #struts.multipart.parser
          =cos
          #指定使用Pell的文件上傳解析器
          #struts.multipart.parser
          =pell
          #指定使用Common-FileUpload的文件上傳解析器
          struts.multipart.parser
          =jakarta


             實(shí)際上,Struts2的文件上傳功能只是在原有的文件上傳項(xiàng)目上做了進(jìn)一步的封裝,取消了不同文件上傳項(xiàng)目的編程差異而已。

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;

          import com.opensymphony.xwork2.ActionSupport;

          public class UploadAction extends ActionSupport {

              
          private static final long serialVersionUID = -3921956808689128362L;
              
          //封裝文件標(biāo)題請(qǐng)求參數(shù)的屬性
              private String title;
              
          //封裝上傳文件域的屬性
              private File upload;
              
          //封裝上傳文件類型的屬性
              private String uploadContentType;
              
          //封裝上傳文件名的屬性
              private String uploadFileName;
              
          //直接在struts.xml文件中配置的屬性
              private String savePath;
              
              
          //文件標(biāo)題的setter和getter方法
              public String getTitle() {
                  
          return title;
              }

              
          public void setTitle(String title) {
                  
          this.title = title;
              }

              
          //上傳文件對(duì)應(yīng)文件內(nèi)容的setter和getter方法
              public File getUpload() {
                  
          return upload;
              }

              
          public void setUpload(File upload) {
                  
          this.upload = upload;
              }

              
          //上傳文件的文件類型的setter和getter方法
              public String getUploadContentType() {
                  
          return uploadContentType;
              }

              
          public void setUploadContentType(String uploadContentType) {
                  
          this.uploadContentType = uploadContentType;
              }

              
          //上傳文件的文件名的setter和getter方法
              public String getUploadFileName() {
                  
          return uploadFileName;
              }

              
          public void setUploadFileName(String uploadFileName) {
                  
          this.uploadFileName = uploadFileName;
              }

              
          //接收struts.xml文件配置值的方法
              public String getSavePath() {
                  
          return savePath;
              }

              
          //返回上傳文件保存位置
              public void setSavePath(String savePath) {
                  
          this.savePath = savePath;
              }

              
              @Override
              
          public String execute() throws Exception {
                  
          //以服務(wù)器的文件保存地址和原有文件名建立上傳文件輸出流
                  FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
                  FileInputStream fis 
          = new FileInputStream(getUpload());
                  
          byte[] buffer = new byte[1024];
                  
          int len = 0;
                  
          while ((len = fis.read(buffer)) > 0{
                      fos.write(buffer, 
          0, len);
                  }

                  
          return SUCCESS;
              }

              
          }


              上面的Action和普通的Action沒有任何區(qū)別。只是多了幾個(gè)屬性而已。如果表單中包含一個(gè)name屬性為xxx的文件域,則對(duì)應(yīng)的Action需要使用3個(gè)屬性來封裝該文件域的信息:類型為File的xxx屬性封裝了該文件的內(nèi)容;類型為String的xxxFileName屬性封裝了該文件域?qū)?yīng)的文件的文件名;類型為String的xxxContentType屬性封裝了該文件域?qū)?yīng)的文件類型。除此之外,配置了savePath來依賴注入文件上傳后的保存地址。具體的Action配置如下:

          <?xml version="1.0" encoding="GBK"?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation/ /DTD Struts Configuration 2.1/ /EN"
              "http://struts.apache.org/dtds/struts-2.1.dtd"
          >
          <struts>
              
          <package name="strutsqs" extends="struts-default">
                  
          <action name="upload" class="com.struts.test.action.UploadAction">
              
          <param name="savePath">/upload</param>        <result>/succ.jsp</result>
                  
          </action>
              
          </package>
          </struts>


              一般我們上傳文件都不是無限制地上傳的,需要制定一些例如文件類型,文件大小等限制。我們可以通過配置Struts2中的攔截器進(jìn)行文件過濾。Struts2中文件上傳的攔截器是fileUpload。配置fileUpload攔截器,可以為其指定兩個(gè)參數(shù):

              allowedTypes:該參數(shù)指定允許上傳文件的類型,多了文件類型之間以英文逗號(hào)隔開。

              maximuxSize:該參數(shù)指定允許上傳文件大小,單位是字節(jié)。

              通過配置fileUpload攔截器,可以輕松地實(shí)現(xiàn)文件過濾,當(dāng)文件過濾失敗后,系統(tǒng)自動(dòng)轉(zhuǎn)入input視圖。當(dāng)然,我們必須顯式地為該Action配置defaultStack的攔截器引用。

          <?xml version="1.0" encoding="GBK"?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation/ /DTD Struts Configuration 2.1/ /EN"
              "http://struts.apache.org/dtds/struts-2.1.dtd"
          >
          <struts>
              
          <package name="strutsqs" extends="struts-default">
                  
          <action name="upload" class="com.struts.test.action.UploadAction">
                      
          <interceptor-ref name="fileUpload">
                                      
          <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                                      
          <param name="maximumSize">2000</param>
                                  
          </interceptor-ref>
                                  
          <interceptor-ref name="defaultStack"/>
                                  
          <param name="savePath">/upload</param>
                      
          <result name="input">/upload.jsp</result>
                      
          <result>/succ.jsp</result>
                  
          </action>
              
          </package>
          </struts>


              如果需要使用Pell上傳, 需要使用插件struts2-pell-multipart-plutin-2.1.6.jar文件。此壓縮包中有一個(gè)PellMultiPartRequest類。同時(shí)在struts-plugin.xml中有如下Bean配置:

                   <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="pell" class="org.apache.struts2.dispatcher.multipart.PellMultiPartRequest" />
             指明了解析接口的實(shí)現(xiàn)類。有了這個(gè)聲明,我們只需在struts.xml中顯式地聲明一下sturts.multipart.parser的值為pell即可:

                   <constant name="struts.multipart.parser" value="pell"/>

                   說道此處,上傳基本說完了。如果需要同時(shí)上傳多了文件,只要將Action中涉及文件上傳的屬性聲明為數(shù)組或List即可。

                   Struts2的文件下載也和普通Action沒有區(qū)別,只是返回的是InputStream流而已。

          import java.io.InputStream;

          import com.opensymphony.xwork2.ActionSupport;
          import org.apache.struts2.ServletActionContext;

          public class FileDownloadAction extends ActionSupport {

              
          private static final long serialVersionUID = 7160810536224371999L;
              
          //該屬性是依賴注入的屬性,可以在配置文件中動(dòng)態(tài)指定該屬性值
              private String inputPath;

              
          public String getInputPath() {
                  
          return inputPath;
              }


              
          public void setInputPath(String inputPath) {
                  
          this.inputPath = inputPath;
              }

              
              
              
          public InputStream getTargetFile() throws Exception {
                   
          return ServletActionContext.getServletContext().getResourceAsStream(inputStream);
              }

          }


              代碼很簡(jiǎn)單,關(guān)鍵是需要配置stream類型的結(jié)果。配置stream類型的結(jié)果需要指定如下4個(gè)屬性:

          • contentType:指定被下載文件的文件類型
          • inputName:指定被下載文件的入口流入流
          • contentDisposition:指定下載的文件名
          • bufferSize:指定下載文件時(shí)的緩沖大小

               

          <action name="download" class="com.struts.test.action.DownloadAction">
              
          <param name="inputPath">\images\t.jpg</param>
              
          <result name="success" type="stream">
                  
          <param name="contentType">image/jpg</param>
                  
          <!-- 指定getTargetFile()方法返回被下載文件的InputStream-->
                  
          <param name="inputName">targetFile</param>
                  
          <param name="contentDisposition">filename="wjc_lgo.jpg"</param>
                  
          <param name="bufferSize">4096</param>
              
          </result>
          </action>

          posted on 2010-11-07 14:19 Brian 閱讀(476) 評(píng)論(0)  編輯  收藏 所屬分類: Struts2

          公告


          導(dǎo)航

          <2010年11月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 来安县| 岳池县| 普兰店市| 汤原县| 偃师市| 合川市| 衡阳市| 萨嘎县| 东乡县| 莱州市| 利辛县| 肇州县| 海安县| 略阳县| 宜都市| 友谊县| 精河县| 苍山县| 遵义市| 武邑县| 嘉禾县| 吴江市| 寿光市| 安化县| 麻阳| 北安市| 东乡县| 平果县| 城固县| 彩票| 丹东市| 长乐市| 柳江县| 东安县| 衡东县| 富锦市| 大同市| 林周县| 铜鼓县| 双城市| 西贡区|