一、上传单个文件
1、首先我们写一个Java类
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class upload {
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"); //此处为上传的路径
if(image!=null){
File saveFile=new File(new File(realPath),imageFileName);
if(!saveFile.getParentFile().exists()){//判断文件夹是否存在,不存在则创建
saveFile.getParentFile().mkdirs();
}
FileUtils.copyFile(image, saveFile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
}
2、jsp页面
<form enctype="multipart/form-data" action="${pageContext.request.contextPath }/test/upload/image" method="post">
<h3>单文件上传</h3>
文件:<input type="file" name="image"><br/>
<input type="submit" value="确定" />
</form>
二、上传多个文件
1、Java类
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class Mupload {
private File [] image;
private String [] imageFileName;//得到上传文件名
//批量上传文件
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.getParentFile().exists()){//判断文件夹是否存在,不存在则创建
savedir.getParentFile().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;
}
}
2、jsp页面
<form enctype="multipart/form-data" action="${pageContext.request.contextPath }/test/upload/images" method="post">
<h3>多文件上传</h3>
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="确定" />
</form>
需要说明的是文件的上传有容量限制,因此我们可以设定上传的大小值,在struts.xml配置文件<struts>中添加如下代码:
<constant name="struts.multipart.maxSize" value="10701096"/>
分享到:
相关推荐
在Struts 2中实现文件上传
在struts 2中实现文件上传 在struts 2中实现文件上传
Struts2中实现文件上传功能Word2007
网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载...
struts与hibernate实现文件的上传与动态下载 主要就是运用struts2.2和hibernate3.6进行文件上传与下载,并且能动态的下载,里面包括了详细的构建文档,以及代码
Struts2框架实现文件上传功能
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2实现的文件上传struts2实现的文件上传struts2实现的文件上传struts2实现的文件上传
struts1和struts2分别实现文件上传下载功能
一个swfupload+struts2实现多文件上传的案例。
自己做的一个struts2 实现文件批量上传功能,
struts2中实现文件上传 struts2中实现CRUD struts2中的OGNL struts2的新表单标志的使用 struts2与AJAX一 struts2与AJAX二 struts2与AJAX三 struts2中用Spring实现IOC struts2中的零配置与IOC struts2介绍之使用连接...
struts2 实现多文件上传功能,是一个很好的学习参考资料
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
网上有些这样的例子,但是下了几个都没有跑起来,哎,希望那些发文章的人要发就发全的,别发个半生不熟的。... 现在自己整理了一个Struts2+ExtJS2实现文异步文件上传,没法上传图片无法看到效果,直接上源码吧。
01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action讲解...09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX