1.UploadAction代码:多文件上传
- packagecom.hxz.action;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importorg.apache.struts2.ServletActionContext;
- importcom.hxz.exception.NameException;
- importcom.opensymphony.xwork2.ActionContext;
- importcom.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("serial")
- publicclassUploadActionextendsActionSupport{
- privateStringtitle;
- privateFile[]upload;
- privateString[]uploadContentType;
- privateString[]uploadFileName;
- privateStringsavePath;
- publicFile[]getUpload(){
- returnupload;
- }
- publicvoidsetUpload(File[]upload){
- this.upload=upload;
- }
- publicString[]getUploadContentType(){
- returnuploadContentType;
- }
- publicvoidsetUploadContentType(String[]uploadContentType){
- this.uploadContentType=uploadContentType;
- }
- publicString[]getUploadFileName(){
- returnuploadFileName;
- }
- publicvoidsetUploadFileName(String[]uploadFileName){
- this.uploadFileName=uploadFileName;
- }
- publicStringgetSavePath()throwsException{
- returnServletActionContext.getRequest().getRealPath(savePath);
- }
- publicvoidsetSavePath(StringsavePath){
- this.savePath=savePath;
- }
- publicStringgetTitle(){
- returntitle;
- }
- publicvoidsetTitle(Stringtitle){
- this.title=title;
- }
- publicStringexecute()throwsException{
- File[]files=getUpload();
- for(inti=0;i<files.length;i++){
- try{
- InputStreamis=newFileInputStream(files[i]);
- Filefile=newFile(this.getSavePath(),this.getUploadFileName()[i]);
- OutputStreamos=newFileOutputStream(file);
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=is.read(buffer))>0){
- os.write(buffer,0,len);
- }
- os.close();
- is.close();
- }catch(Exceptione){
- e.printStackTrace();
- thrownewNameException("上传文件错误!");
- }
- }
- return"zhe";
- }
- }
2.upload.jsp
- <%@pagelanguage="java"pageEncoding="GBK"%>
- <%@taglibprefix="s"uri="/struts-tags"%>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <title>UploadFile</title>
- </head>
- <body>
- <formaction="uploadMany.action"method="post"enctype="multipart/form-data">
- <s:fielderror/>
- ${requestScope.typeError}<br>
- 文件标题:<inputtype="text"name="title"/><br>
- 选择1文件:<inputtype="file"name="upload"/><br>
- 选择2文件:<inputtype="file"name="upload"/><br>
- 选择3文件:<inputtype="file"name="upload"/><br>
- <inputtype="submit"value="上传"/>
- </form>
- </body>
- </html>
3.strust.xml,采用fileUpload拦截器实现文件类型和大小验证
- <?xmlversion="1.0"encoding="GBK"?>
- <!DOCTYPEstrutsPUBLIC
- "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constantname="struts.custom.i18n.resources"value="globalMessages"/>
- <constantname="struts.i18n.encoding"value="utf-8"/>
- <packagename="lee"extends="json-default">
- <global-exception-mappings>
- <!--指Action抛出AuctionException异常时,转入名为exception的结果。-->
- <exception-mappingexception="com.hxz.excepton.NameException"result="exception"/>
- <!--指Action抛出Exception异常时,转入名为exception的结果。-->
- <exception-mappingexception="java.lang.Exception"result="exception"/>
- </global-exception-mappings>
- <actionname="readName"class="com.hxz.action.NameAction">
- <resultname="success">/testJstl.jsp</result>
- <resultname="error">/error.jsp</result>
- </action>
- <actionname="getNameById"class="com.hxz.action.NameAction"method="getNameById">
- <resultname="success"type="json">/name.html</result>
- <resultname="error">/error.jsp</result>
- </action>
- <actionname="updateName"class="com.hxz.action.NameAction"method="updateName">
- <resultname="success">/name.html</result>
- <resultname="error">/error.jsp</result>
- </action>
- <actionname="upload"class="com.hxz.action.UploadAction">
- <interceptor-refname="fileUpload">
- <paramname="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param>
- <paramname="maximumSize">2000</param>
- </interceptor-ref>
- <interceptor-refname="defaultStack"/>
- <paramname="savePath">/upload</param>
- <resultname="zhe">/upload/succ.jsp</result>
- <resultname="input">/upload/upload.jsp</result>
- </action>
- </package>
- </struts>
4.成功页面succ.jsp
- <%@pagelanguage="java"pageEncoding="GBK"%>
- <%@taglibprefix="s"uri="/struts-tags"%>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <title>succ.jsp</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- </head>
- <body>
- 文件标题:<s:propertyvalue="+title"/><br>
- <imgsrc="<s:propertyvalue="uploadFileName[0]"/>"/>
- <imgsrc="<s:propertyvalue="uploadFileName[1]"/>"/>
- <imgsrc="<s:propertyvalue="uploadFileName[2]"/>"/>
- </body>
- </html>