Struts2:多文件上传
2015-10-14 09:29
357 查看
源码下载地址:http://download.csdn.net/detail/zhoujing_0424/9239429
在Struts2中实现多文件上传,分为三步:
1.第一步
在WEB-INF/lib下加入commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar,这两个文件可以从http://commons.apache.org下载。
2.第二步
把form表的enctype设置为multipart/form-data,本例中myform.jsp代码如下:
3.第三步
在Action类中添加以下属性,属性对应于表单中文件字段的名称。本例中HelloWorldAction.java代码如下:
hello.jsp用于显示文件是否上传成功,代码如下:
struts.xml文件中的配置如下:
在Struts2中实现多文件上传,分为三步:
1.第一步
在WEB-INF/lib下加入commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar,这两个文件可以从http://commons.apache.org下载。
2.第二步
把form表的enctype设置为multipart/form-data,本例中myform.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'myform.jsp' starting page</title> </head> <body> <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/control/helloworld_execute.action" method="post"> 文件1:<input type="file" name="upLoadImage"><br> 文件2:<input type="file" name="upLoadImage"><br> <input type="submit" value="上传"> </form> </body> </html>注意:input中的name值应相同,且与对应Action类中的相应属性同名。
3.第三步
在Action类中添加以下属性,属性对应于表单中文件字段的名称。本例中HelloWorldAction.java代码如下:
package cn.zj.action; import java.io.File; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionContext; public class HelloWorldAction { private File[] upLoadImage; private String[] upLoadImageFileName;//得到上传文件的名称 public File[] getUpLoadImage() { return upLoadImage; } public void setUpLoadImage(File[] upLoadImage) { this.upLoadImage = upLoadImage; } public String[] getUpLoadImageFileName() { return upLoadImageFileName; } public void setUpLoadImageFileName(String[] upLoadImageFileName) { this.upLoadImageFileName = upLoadImageFileName; } public String execute() throws IOException{ String realPath=ServletActionContext.getServletContext().getRealPath("/images"); if(upLoadImage!=null){ File saveDir=new File(realPath); if(!saveDir.exists()) saveDir.mkdirs(); for(int i=0;i<upLoadImage.length;i++){ File saveFile=new File(saveDir,upLoadImageFileName[i]); FileUtils.copyFile(upLoadImage[i], saveFile); } ActionContext.getContext().put("message", "上传成功"); } return "success"; } }
hello.jsp用于显示文件是否上传成功,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP 'hello.jsp' starting page</title> </head> <body> ${requestScope.message } <br> </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> <!-- 通过struts.action.extension常量修改默认URL后缀.action为.do或.action --> <constant name="struts.action.extension" value="do,action"></constant> <!-- 设置上传文件的大小限制 --> <constant name="struts.multipart.maxSize" value="10701096"></constant> <!-- 当struts配置文件修改后,系统是否重新加载该文件,默认值为false,开发阶段最好打开,这样就无需经常restart server --> <constant name="struts.configuration.xml.reload" value="true"></constant> <package name="zj" namespace="/control" extends="struts-default<span style="font-family: Arial, Helvetica, sans-serif;">"> </span> <!-- dispatcher内部请求转发方式 --> <action name="helloworld_*" class="cn.zj.action.HelloWorldAction" method="{1}"> <!-- 使用通配符指定action的method --> <result name="success">/WEB-INF/page/hello.jsp</result> </action> </package> </struts>在浏览器地址栏中输入http://localhost:8080/Struts2/myform.jsp,选择文件并点击“上传”,若上传成功,则跳转到http://localhost:8080/Struts2/control/helloworld_execute.action,显示“上传成功”。若文件超过大小限制,则可通过在struts.xml文件中修改常量struts.multipart.maxSize来设置上传文件的大小限制,如上述代码所示。
相关文章推荐
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- Java面向对象的五个基本原则
- JDK下载安装与配置
- 构建Maven工程(2)---MyEclipse安装maven插件
- Spring中配置数据源的4种形式
- 总结struts2 iterator status的用法
- MODBUS协议 IEEE32位二进制浮点数与十进制小数转换的方法等内容
- Java常见异常(Runtime Exception )小结
- spring事务的传播特性
- Java HashMap 原理
- Java读取Properties文件的六种方法 .
- 关于struts2的!号
- myeclipse 编辑JS很慢的解决办法
- Spring部分注解的个人简单理解
- springMVC与struts2的区别
- java File删除文件夹完整版
- eclipse访问Android中的/data/目录
- MyEclipse6.5常用快捷键
- Myeclipse SVN安装
- windows 中 Eclipse 打开当前文件所在文件夹