新聞中心
【相關(guān)文章】

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都做網(wǎng)站、萬源網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、萬源網(wǎng)絡(luò)營銷、萬源企業(yè)策劃、萬源品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供萬源建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
- Struts2教程1:第一個(gè)Struts2程序
- Struts2教程2:處理一個(gè)form多個(gè)submit
- Struts2教程3:struts.xml常用配置解析
- Struts2教程4:使用validate方法驗(yàn)證數(shù)據(jù)
- Struts2教程5:使用Validation框架驗(yàn)證數(shù)據(jù)
- Struts2教程6:在Action類中獲得HttpServletResponse對象
- Struts2教程8:攔截器概述
- Struts2教程9:實(shí)現(xiàn)自已的攔截器
- Struts2教程10:國際化
上傳單個(gè)文件
上傳文件是很多Web程序都具有的功能。在Struts1.x中已經(jīng)提供了用于上傳文件的組件。而在Struts2中提供了一個(gè)更為容易操作的上傳文件組件。所不同的是,Struts1.x的上傳組件需要一個(gè)ActionForm來傳遞文件,而Struts2的上傳組件是一個(gè)攔截器(這個(gè)攔截器不用配置,是自動裝載的)。在本文中先介紹一下如何用struts2上傳單個(gè)文件,最后介紹一下用struts2上傳任意多個(gè)文件。
要用Struts2實(shí)現(xiàn)上傳單個(gè)文件的功能非常容易實(shí)現(xiàn),只要使用普通的Action即可。但為了獲得一些上傳文件的信息,如上傳文件名、上傳文件類型以及上傳文件的Stream對象,就需要按著一定規(guī)則來為Action類增加一些getter和setter方法。
在Struts2中,用于獲得和設(shè)置java.io.File對象(Struts2將文件上傳到臨時(shí)路徑,并使用java.io.File打開這個(gè)臨時(shí)文件)的方法是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方法中的實(shí)現(xiàn)代碼就很簡單了,只是從臨時(shí)文件復(fù)制到指定的路徑(在這里是d:upload)中。上傳文件的臨時(shí)目錄的默認(rèn)值是javax.servlet.context.tempdir的值,但可以通過struts.properties(和struts.xml在同一個(gè)目錄下)的struts.multipart.saveDir屬性設(shè)置。Struts2上傳文件的默認(rèn)大小限制是2M(2097152字節(jié)),也可以通過struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上傳文件的總大小不能超過2K字節(jié)。
下面的代碼是上傳文件的JSP頁面代碼:
也可以在success.jsp頁中通過<s:property>獲得文件的屬性(文件名和文件內(nèi)容類型),代碼如下:
#p#
上傳任意多個(gè)文件
在Struts2中,上傳任意多個(gè)文件也非常容易實(shí)現(xiàn)。首先,要想上傳任意多個(gè)文件,需要在客戶端使用DOM技術(shù)生成任意多個(gè)標(biāo)簽。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>
<inputtype="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>
?。糹nputtype="submit"value="上傳"/>
?。?form>
</body>
</html>
上面的javascript代碼可以生成任意多個(gè)<input type=’file’>標(biāo)簽,name的值都為file(要注意的是,上面的javascript代碼只適合于IE瀏覽器,firefox等其他瀏覽器需要使用他的代碼)。至于Action類,和上傳單個(gè)文件的Action類基本一至,只需要將三個(gè)屬性的類型改為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對象進(jìn)行枚舉,在循環(huán)中的代碼和上傳單個(gè)文件時(shí)的代碼基本相同。如果讀者使用過struts1.x的上傳組件,是不是感覺Struts2的上傳功能更容易實(shí)現(xiàn)呢?在Struts1.x中上傳多個(gè)文件時(shí),可是需要建立帶索引的屬性的。而在Struts2中,就是這么簡單就搞定了。圖1是上傳任意多個(gè)文件的界面。
圖1
【編輯推薦】
- Struts2教程1:第一個(gè)Struts2程序
- Struts2教程2:處理一個(gè)form多個(gè)submit
- Struts2教程3:struts.xml常用配置解析
- Struts2教程4:使用validate方法驗(yàn)證數(shù)據(jù)
- Struts2教程5:使用Validation框架驗(yàn)證數(shù)據(jù)
- Struts2教程6:在Action類中獲得HttpServletResponse對象
- Struts2教程8:攔截器概述
- Struts2教程9:實(shí)現(xiàn)自已的攔截器
- Struts2教程10:國際化
文章名稱:Struts2教程7:上傳任意多個(gè)文件
當(dāng)前地址:http://www.dlmjj.cn/article/copceoh.html


咨詢
建站咨詢
