Struts2 文件上传

本文详细介绍使用Struts2框架实现文件上传的具体步骤,包括所需jar包配置、表单设置及Action类编写,并演示了单文件与多文件上传的实现方式。
文件上传分为三个步骤:
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file" name="image">
</form>
index.jsp页面如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP 'add.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<form enctype="multipart/form-data" action="<=request.getContextPath() %>/renwen/test/helloworld" method="post">
<input type="file" name="image">
<input type="submit" value="上传">
</form>
</body>
</html>
第三步:在Action类中添加以下属性,属性对应于表单中文件字段的名称
action代码如下:
package com.renwen.action;
import java.io.File;
import java.net.URLEncoder;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.renwen.bean.Person;
public class HelloWorldAction {
//属性名和jsp页面上字段名保持一致
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;
}
public String execute() throws Exception{
//保存路径
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
File savefile = new File(new File(realpath),imageFileName);
//判断上传文件是否为空
if(image!=null){
//判断路径是否存在
if(!savefile.getParentFile().exists()){
//创建路径
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
//返回信息
ActionContext.getContext().put("message", "上传成功");
}
}
return "success";
}
}
关于多个文件的上传可以利用数组:
jsp页面修改如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP 'add.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<form enctype="multipart/form-data" action="<=request.getContextPath() %>/renwen/test/helloworld" method="post">
文件1: <input type="file" name="image">
文件2: <input type="file" name="image">
文件3: <input type="file" name="image">
<input type="submit" value="上传">
</form>
</body>
</html>
action修改如下:
package com.renwen.action;
import java.io.File;
import java.net.URLEncoder;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.renwen.bean.Person;
public class HelloWorldAction {
//属性名和jsp页面上字段名保持一致
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;
}
public String execute() throws Exception{
//保存路径
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);

//判断上传文件是否为空
if(image!=null){
File file = new File(realpath)
//判断路径是否存在
if(!file.exists())file.mkdirs()
for(int i = 0;i<image.length;i++)
{
File savefile = new File(new File(realpath),imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
//返回信息
ActionContext.getContext().put("message", "上传成功");
}
}
return "success";
}
}
测试以上代码OK
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值