偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Struts2教程7:上傳任意多個文件

開發(fā) 開發(fā)工具 后端
Struts是Apache基金會Jakarta項目組的一個Open Source項目,它采用MVC模式,能夠很好地幫助Java開發(fā)者利用J2EE開發(fā)Web應(yīng)用。和其他的Java架構(gòu)一樣,Struts也是面向?qū)ο笤O(shè)計,將MVC模式"分離顯示邏輯和業(yè)務(wù)邏輯"的能力發(fā)揮得淋漓盡致。Struts的目的是為了減少在運用MVC設(shè)計模型來開發(fā)Web應(yīng)用的時間。你仍然需要學習和應(yīng)用該架構(gòu),不過它將可以完成其中一些繁重的工作。在本系列教程中我們將學習到Struts2的各種技術(shù)。

【相關(guān)文章】

  1. Struts2教程1:第一個Struts2程序
  2. Struts2教程2:處理一個form多個submit
  3. Struts2教程3:struts.xml常用配置解析
  4. Struts2教程4:使用validate方法驗證數(shù)據(jù)
  5. Struts2教程5:使用Validation框架驗證數(shù)據(jù)
  6. Struts2教程6:在Action類中獲得HttpServletResponse對象
  7. Struts2教程8:攔截器概述
  8. Struts2教程9:實現(xiàn)自已的攔截器
  9. Struts2教程10:國際化

上傳單個文件

上傳文件是很多Web程序都具有的功能。在Struts1.x中已經(jīng)提供了用于上傳文件的組件。而在Struts2中提供了一個更為容易操作的上傳文件組件。所不同的是,Struts1.x的上傳組件需要一個ActionForm來傳遞文件,而Struts2的上傳組件是一個攔截器(這個攔截器不用配置,是自動裝載的)。在本文中先介紹一下如何用struts2上傳單個文件,最后介紹一下用struts2上傳任意多個文件。

要用Struts2實現(xiàn)上傳單個文件的功能非常容易實現(xiàn),只要使用普通的Action即可。但為了獲得一些上傳文件的信息,如上傳文件名、上傳文件類型以及上傳文件的Stream對象,就需要按著一定規(guī)則來為Action類增加一些getter和setter方法。

在Struts2中,用于獲得和設(shè)置java.io.File對象(Struts2將文件上傳到臨時路徑,并使用java.io.File打開這個臨時文件)的方法是getUpload和setUpload。獲得和設(shè)置文件名的方法是getUploadFileName和setUploadFileName,獲得和設(shè)置上傳文件內(nèi)容類型的方法是getUploadContentType和setUploadContentType。下面是用于上傳的動作類的完整代碼:

packageaction;

importjava.io.*;
importcom.opensymphony.xwork2.ActionSupport;
publicclassUploadActionextendsActionSupport
{
  privateFileupload;
  privateStringfileName;
  privateStringuploadContentType;
  
  publicStringgetUploadFileName()
  {
    returnfileName;
  }
  publicvoidsetUploadFileName(StringfileName)
  {
    this.fileName=fileName;
  }
  publicFilegetUpload()
  {
    returnupload;
  }
  publicvoidsetUpload(Fileupload)
  {
    this.upload=upload;
  }
  publicvoidsetUploadContentType(StringcontentType)
  {
    this.uploadContentType=contentType;
  
  }
  
  publicStringgetUploadContentType()
  {
    returnthis.uploadContentType;
  }
  publicStringexecute()throwsException
  { 
    java.io.InputStreamis=newjava.io.FileInputStream(upload);
    java.io.OutputStreamos=newjava.io.FileOutputStream("d:upload"+fileName);
    bytebuffer[]=newbyte[8192];
    intcount=0;
    while((count=is.read(buffer))>0)
    {
      os.write(buffer,0,count);
    }
    os.close();
    is.close();
    returnSUCCESS;
  }
}

在execute方法中的實現(xiàn)代碼就很簡單了,只是從臨時文件復(fù)制到指定的路徑(在這里是d:upload)中。上傳文件的臨時目錄的默認值是javax.servlet.context.tempdir的值,但可以通過struts.properties(和struts.xml在同一個目錄下)的struts.multipart.saveDir屬性設(shè)置。Struts2上傳文件的默認大小限制是2M(2097152字節(jié)),也可以通過struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上傳文件的總大小不能超過2K字節(jié)。

下面的代碼是上傳文件的JSP頁面代碼:

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<html>
  <head>
   ?。紅itle>上傳單個文件</title>
 ?。?head>
 ?。糱ody>
   ?。約:formaction="upload"namespace="/test"
      enctype="multipart/form-data">
     ?。約:filename="upload"label="輸入要上傳的文件名"/>
     ?。約:submitvalue="上傳"/>
    </s:form>
 ?。?body>
</html>

也可以在success.jsp頁中通過<s:property>獲得文件的屬性(文件名和文件內(nèi)容類型),代碼如下:

<s:property value="uploadFileName"/>

#p#

上傳任意多個文件

在Struts2中,上傳任意多個文件也非常容易實現(xiàn)。首先,要想上傳任意多個文件,需要在客戶端使用DOM技術(shù)生成任意多個標簽。name屬性值都相同。代碼如下:

<html>
 ?。糷ead>
   ?。約criptlanguage="javascript">
functionaddComponent()
{
    varuploadHTML=document.createElement("<inputtype='file' name='upload'/>");
    document.getElementById("files").appendChild(uploadHTML);
    uploadHTML=document.createElement("<p/>");
    document.getElementById("files").appendChild(uploadHTML);
}
</script>
 ?。?head>
 ?。糱ody>
   ?。糹nputtype="button"onclick="addComponent();"value="添加文件"/>
   ?。糱r/>
   ?。糵ormonsubmit="returntrue;"action="/struts2/test/upload.action"
      method="post"enctype="multipart/form-data">
     ?。約panid="files"><inputtype='file'name='upload'/>
       ?。紁/>
     ?。?span>
      <inputtype="submit"value="上傳"/>
    </form>
 ?。?body>
</html>

上面的javascript代碼可以生成任意多個<input type=’file’>標簽,name的值都為file(要注意的是,上面的javascript代碼只適合于IE瀏覽器,firefox等其他瀏覽器需要使用他的代碼)。至于Action類,和上傳單個文件的Action類基本一至,只需要將三個屬性的類型改為List即可。代碼如下:

packageaction;

importjava.io.*;
importcom.opensymphony.xwork2.ActionSupport;
publicclassUploadMoreActionextendsActionSupport
{
  privatejava.util.List<File>uploads;
  privatejava.util.List<String>fileNames;
  privatejava.util.List<String>uploadContentTypes;
  publicjava.util.List<String>getUploadFileName()
  {
    returnfileNames;
  }
  publicvoidsetUploadFileName(java.util.List<String>fileNames)
  {
    this.fileNames=fileNames;
  }
  publicjava.util.List<File>getUpload()
  {
    returnuploads;
  }
  publicvoidsetUpload(java.util.List<File>uploads)
  {
    this.uploads=uploads;
  }
  publicvoidsetUploadContentType(java.util.List<String>contentTypes)
  {
    this.uploadContentTypes=contentTypes;
  }
  publicjava.util.List<String>getUploadContentType()
  {
    returnthis.uploadContentTypes;
  }
  publicStringexecute()throwsException
  {
    if(uploads!=null)
    {
      inti=0;
      for(;i<uploads.size();i++)
      {
        java.io.InputStreamis=newjava.io.FileInputStream(uploads.get(i));
        java.io.OutputStreamos=newjava.io.FileOutputStream(
            "d:upload"+fileNames.get(i));
        bytebuffer[]=newbyte[8192];
        intcount=0;
        while((count=is.read(buffer))>0)
        {
          os.write(buffer,0,count);
        }
        os.close();
        is.close();
      }
    }
    returnSUCCESS;
  }
}

在execute方法中,只是對List對象進行枚舉,在循環(huán)中的代碼和上傳單個文件時的代碼基本相同。如果讀者使用過struts1.x的上傳組件,是不是感覺Struts2的上傳功能更容易實現(xiàn)呢?在Struts1.x中上傳多個文件時,可是需要建立帶索引的屬性的。而在Struts2中,就是這么簡單就搞定了。圖1是上傳任意多個文件的界面。

 
圖1

【編輯推薦】

  1. Struts2教程1:第一個Struts2程序
  2. Struts2教程2:處理一個form多個submit
  3. Struts2教程3:struts.xml常用配置解析
  4. Struts2教程4:使用validate方法驗證數(shù)據(jù)
  5. Struts2教程5:使用Validation框架驗證數(shù)據(jù)
  6. Struts2教程6:在Action類中獲得HttpServletResponse對象
  7. Struts2教程8:攔截器概述
  8. Struts2教程9:實現(xiàn)自已的攔截器
  9. Struts2教程10:國際化
責任編輯:楊鵬飛 來源: BlogJava
相關(guān)推薦

2009-06-25 15:50:03

Struts2教程上傳任意多個文件

2009-06-04 09:41:50

struts2上傳文件

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 11:00:44

2009-06-08 16:44:00

Struts2文件上傳

2009-06-25 15:22:03

Struts2教程一個form多個sub

2009-02-04 10:51:07

2009-06-03 14:19:34

Struts2Guice

2009-06-25 16:04:30

2009-06-25 15:26:25

Struts2教程struts.xml常

2009-06-04 08:34:24

Struts2配置struts.xml

2009-02-04 15:04:13

2009-06-25 15:54:42

Struts2教程攔截器

2009-02-04 11:37:15

2009-07-29 09:54:34

struts2和str

2009-02-04 14:19:38

2009-06-25 15:37:12

Struts2教程Validation框

2009-06-25 15:33:12

Struts2教程使用validate驗證數(shù)據(jù)

2009-06-25 15:59:21

Struts2教程攔截器

2009-02-04 12:00:08

點贊
收藏

51CTO技術(shù)棧公眾號