您的位置:首页 > 编程语言 > Java开发

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代码如下:

<%@ 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来设置上传文件的大小限制,如上述代码所示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: