struts2进行多文件上传

首先搭建好struts2的开发环境,导入struts2需要的最少jar包


新建upload.jsp页面,注意一定要把表单的enctype设置成multipart/form-data

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'upload.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
  	<s:fielderror name="fieldErrors"></s:fielderror>
    <s:form theme="simple" action="upload" enctype="multipart/form-data" method="post">
    	file:<s:file name="file"></s:file>
    	fileDesc:<s:textfield name="fileDesc[0]"></s:textfield>
    	<br/><br/>
    	file:<s:file name="file"></s:file>
    	fileDesc:<s:textfield name="fileDesc[1]"></s:textfield>
    	<br/><br/>
    	file:<s:file name="file"></s:file>
    	fileDesc:<s:textfield name="fileDesc[2]"></s:textfield>
    	<s:submit></s:submit>
    </s:form>
  </body>
</html>

新建一个UploadAction类,这个类主要有三个属性,并为这三个属性生成对应的set get方法

  • [File Name] : 保存要上传的文件

  • [File Name]ContentType : 保存要上传的文件类型

  • [File Name]FileName :保存上传的文件名

package cn.lfd.web.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/*
 * 多文件上传要把对应的属性类型都改为List集合,struts自动会把多个文件的数据封装到里面
 */
public class UploadAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private List<File> file;
	private List<String> fileContentType;
	private List<String> fileFileName;
	private List<String> fileDesc;
	
	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileDesc() {
		return fileDesc;
	}

	public void setFileDesc(List<String> fileDesc) {
		this.fileDesc = fileDesc;
	}

	@Override
	public String execute() throws Exception {
		//遍历文件集合,通过IO流把每一个上传的文件保存到upload文件夹下面
		for(int i=0;i<file.size();i++) {
			//得到要保存的文件路径
			String dir = ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName.get(i));
			OutputStream out = new FileOutputStream(dir);
			InputStream in = new FileInputStream(file.get(i));
			byte[] flush = new byte[1024];
			int len = 0;
			while((len=in.read(flush))!=-1) {
				out.write(flush, 0, len);
			}
			in.close();
			out.close();
		}
		return "input";
	}
}

然后在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>
	<constant name="struts.custom.i18n.resources" value="message"></constant>
	<package name="default" extends="struts-default">
		<interceptors>
			<interceptor-stack name="lfdstack">
				<interceptor-ref name="defaultStack">
					<param name="fileUpload.maximumSize">200000</param><!-- 限制上传的单个文件的大小 -->
					<param name="fileUpload.allowedTypes">text/html,text/xml</param><!-- 限制上传的文件的类型 -->
					<param name="fileUpload.allowedExtensions">txt,html,xml</param><!-- 限制上传的文件的扩展名 -->
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="lfdstack"></default-interceptor-ref>
		<action name="upload" class="cn.lfd.web.upload.UploadAction">
			<result>/success.jsp</result>
			<result name="input">/upload.jsp</result>
		</action>
	</package>
</struts>

在src目录下新建一个message.properties文件定制错误消息

  • struts.messages.error.uploading - 文件不能被上传

  • struts.messages.error.file.too.large - 文件超出大小

  • struts.messages.error.content.type.not.allowed - 文件类型不合法

  • struts.messages.error.file.extension.not.allowed - 文件扩展名不合法

显示效果如下图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值