Struts2:多文件上传

源码下载地址: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代码如下:

<%@ 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来设置上传文件的大小限制,如上述代码所示。






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值