多文件上传的处理步骤同多文件上传(加入jar文件、写form、写Action)
form表单:3个文件的名称要是一样的
<form action="${pageContext.request.contextPath }/neu/upload_execute.action" enctype="multipart/form-data" 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>
Action类:与单文件相比,只是将上传文件和上传文件名变成文件数组,在方法里使用循环进行创建文件
public class Upload {
private File[] image;//此处要与jsp页面上的name的值一样,得到上传的文件
private String[] imageFileName; //得到文件的名称 命名规则:字段名称+FileName
private String imageContentType;//得到文件的类型 命名规则:字段名称+ContentType
public String execute() throws Exception {
//得到绝对路径
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
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);
}
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
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;
}
}
分享到:
相关推荐
struts2多文件上传
我的Struts2多文件上传实例 博文链接:https://zmx.iteye.com/blog/461026
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
struts2多文件上传显示进度 无插件
AjaxFileUpload Struts2 多文件上传
struts2 多文件上传下载 Java 文件上传下载 有界面,文件流实现上传下载(可多文件)
Struts2 文件上传下载 多文件 单文件 实例;支持单个或多个文件的上传下载,并且自定义上传文件的大小和类型!!!
uploadify+struts2多文件上传实例,解决了在firefox下的session问题,相信能帮助到你的
文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...
ext结合struts2.0实现文件上传,代码详细,加载项目即可以看到效果。