文件上传大体我给分这么几步。1:在WEB-INF/lib下面添加commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,这两个文件全部在Struts2提供的lib包下面;2:在文件上传的表单处做设置,将form表单的enctype设置成为:“multipart/form-data”,提交方式设置为post;3:做Action。
具体代码如下:
fileupload.jsp
struts.xml
FileUploadAction.java
message.jsp
几点注意事项:在Action中的属性,比如image,都必须跟表单当中的属性值一样,都是image,还有属性的名字都必须按照固定的格式写,如image,imageFileName,如果不这么写,会报错的。
具体代码如下:
fileupload.jsp
![](http://dl.iteye.com/upload/attachment/206450/0eab6cca-492b-3700-a684-e1b99b67cb7b.jpg)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${pageContext.request.contextPath}<br />
<form action="${pageContext.request.contextPath }/fileupload" enctype="multipart/form-data" method="post">
选择资源1:<input type="file" name="image"><BR />
选择资源2:<input type="file" name="image"><BR />
<input type="submit" value="上传"/>
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="yuan" extends="struts-default">
<action name="fileupload" class="com.ambow.Action.FileUploadAction">
<result>
/WEB-INF/page/message.jsp
</result>
</action>
</package>
</struts>
FileUploadAction.java
package com.ambow.Action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
//这里是固定的写法,文件名子必须跟上一个表单的name属性名字相同,都是image
private File[] image ;
private String[] imageFileName ;
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
//得到当前工程的目录,然后自己再定义下保存的路径
String realpath = ServletActionContext.getServletContext().getRealPath("/file") ;
//打印路径看是否存在
System.out.println(realpath);
if(image != null){
File savedir = new File(realpath) ;
if(!savedir.exists()){
savedir.mkdirs() ;
}
for(int i=0;i<image.length;i++){
File savefile = new File(savedir,imageFileName[i]) ;
FileUtils.copyFile(image[i], savefile) ;
}
}
ServletActionContext.getServletContext().setAttribute("message", "文件上传成功!") ;
return SUCCESS;
}
}
message.jsp
![](http://dl.iteye.com/upload/attachment/206452/825d4c2b-abfc-3fde-a1c0-809a8a4c3e50.jpg)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>
几点注意事项:在Action中的属性,比如image,都必须跟表单当中的属性值一样,都是image,还有属性的名字都必须按照固定的格式写,如image,imageFileName,如果不这么写,会报错的。