1. web.xml 写全文拦截器
WEBROOT--- WEB-INF ---- WEB.XML
Struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Struts2
/*
2.写个文件上传页面
WEBROOT--E文件夹下----- upload.jsp
用户名:文件:
3. 项目文件下 struts.xml
4. 项目src下 建立包: cn.itcast.e_fileupload 下 新建 upload.xml
/p>
"http://struts.apache.org/dtds/struts-2.3.dtd">
/e/success.jsp
5. cn.itcast.e_fileupload 下新建 fileupload.java
packagecn.itcast.e_fileupload;importjava.io.File;importorg.apache.commons.io.FileUtils;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;public class fileupload extendsActionSupport{privateFile file1;privateString file1FileName; //该变量 固定格式 "file1" + FileName(注意大小写)privateString file1ContextType; //该变量 固定格式public voidsetFile1(File file1) {this.file1 =file1;
}public voidsetFile1filename(String file1FileName) {this.file1FileName =file1FileName;
}public voidsetFile1contexttype(String file1ContextType) {this.file1ContextType =file1ContextType;
}
@Overridepublic String execute() throwsException {
String path=ServletActionContext.getServletContext().getRealPath("/upload");
File desfile=newFile(path,file1FileName);
FileUtils.copyFile(file1, desfile);returnSUCCESS;
}
}