1、首先导入commons-io-1.3.2.jar还有commons-fileupload-1.2.1.jar两个jar包,然后在web.xml文件中加入如下代码:
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、
写上传的index.jsp文件:
<form action="upload.action"method="post"
enctype="multipart/form-data">
<input type="text"name="title"/>
<br>
<input type="file"name="upload"/>
<br>
<input value="upload"type="submit"/>
</form>
3、
Action代码:
publicclass UploadAction extends ActionSupport {
private String title;//文件标题,对应form表单的title
private File upload;//文件内容,对应form表单的upload
private String uploadContentType;//文件类型
private String uploadFileName;//文件名
private String allowTypes;//允许的类型
private String savePath;//保存路径
// 接受依赖注入的方法
publicvoid setSavePath(String value) {
this.savePath = value;
}
private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
publicvoid setTitle(String title) {
this.title = title;
}
publicvoid setUpload(File upload) {
this.upload = upload;
}
publicvoid setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
publicvoid setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File getUpload() {
return (this.upload);
}
public StringgetUploadContentType() {
return (this.uploadContentType);
}
public String getUploadFileName(){
return (this.uploadFileName);
}
@Override
public String execute() throws Exception {
System.out.println("开始上传单个文件---");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = newbyte[1024];
int len = 0;
while ((len = fis.read(buffer))> 0) {
fos.write(buffer, 0, len);
}
returnSUCCESS;
}
public String filterType(String[]types) {
String fileType = this.getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
returnnull;
}
}
returnINPUT;
}
public String getAllowTypes() {
returnallowTypes;
}
publicvoid setAllowTypes(String allowTypes) {
this.allowTypes =allowTypes;
}
}
4、
Struts.xml代码:
<constant name="struts.multipart.maxSize" value="20971520"/>
<constant name="struts.custom.i18n.resources"
value="globalMessages"/>
<constant name="struts.i18n.encoding"value="GBK"/>
<package name="lee"extends="struts-default">
<action name="upload"class="UploadAction">
<param name="savePath">/upload</param>
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
5、
返回结果的success.jsp:
<body>
上传成功!
<br>
文件标题:
<s:property value=" +title" />
<br>
文件为:
<s:property value="'upload/'+ uploadFileName"/>
<br>
</body>