用struts2来写文件上传是非常简单。
一、单文件上传
第一步写一个表单
<body>
<form action="${pageContext.request.contextPath }/upload/UploadAction" method="post" enctype="multipart/form-data">
文件上传<input type="file" name="image"><br>
<input type="submit" value="上传">
</form>
</body>
第二步在struts.xml添加
<package name="upload" namespace="/upload" extends="mypackage">
<action name="UploadAction" class="cn.hhtc.action.UploadAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
第三步编写类UploadAction
package cn.hhtc.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport implements Serializable {
private File image; //表单输入的文件
private String imageFileName;//获取文件名
private String imageContentType;//获得上传文件的类型MIME类型
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;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String execute()
{
try {
System.out.println(imageContentType);
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath("/files");
//构建输入输出流
// OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);
// InputStream in = new FileInputStream(image);
// byte[] b = new byte[1024];
// int len = -1;
// while((len=in.read(b))!=-1)
// {
// out.write(b, 0, len);
// }
// in.close();
// out.close();
FileUtils.copyFile(image, new File(storePath, imageFileName));//代替上面的
ActionContext.getContext().put("message", "文件上传成功");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
第四步,success.jsp
<body>
文件上传成功
</body>
注意一下 UploadAction里面的字段要跟表单里面的字段一致,文件名字段为 表单文件名+FileName,文件类型为 表单文件名+ContentType
二、多文件上传
第一步:写一个表单
<body>
<form action="${pageContext.request.contextPath }/upload2/UploadAction2" method="post" enctype="multipart/form-data">
文件上传<input type="file" name="images"><br>
文件上传<input type="file" name="images"><br>
<input type="submit" value="上传">
</form>
</body>
第二步配置struts.xml
<package name="upload2" namespace="/upload2" extends="mypackage">
<action name="UploadAction2" class="cn.hhtc.action.UploadAction2" method="execute1">
<result name="success">/success.jsp</result>
</action>
</package>
第三步写UploadAction2类
package cn.hhtc.action;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction2 extends ActionSupport implements Serializable {
private File[] images;
private String[] imagesFileName;
private String[] imagesContentType;
public File[] getImages() {
return images;
}
public void setImages(File[] images) {
this.images = images;
}
public String[] getImagesFileName() {
return imagesFileName;
}
public void setImagesFileName(String[] imagesFileName) {
this.imagesFileName = imagesFileName;
}
public String[] getImagesContentType() {
return imagesContentType;
}
public void setImagesContentType(String[] imagesContentType) {
this.imagesContentType = imagesContentType;
}
public String execute1()
{
try {
if(images!=null &&images.length>0)
{
ServletContext sc = ServletActionContext.getServletContext();
String storeFile = sc.getRealPath("/files");
for(int i=0; i<images.length; i++)
{
FileUtils.copyFile(images[i], new File(storeFile, imagesFileName[i]));
}
}
ActionContext.getContext().put("message", "<font color='red'>文件上传成功</font>");
return SUCCESS;
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}
}
}