Eclipse+Maven+Struts2+strusts拦截器实现上传多个文件

废话就不多说了,先看效果:


选择上传的文件,几个都没关系

点击上传后 提示上传成功!



指定的目录下就有了你上传的文件:

教程:


1.新建一个Maven项目,在pom.xml中引入struts 2.5的核心依赖包:



2.配置web.xml

  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

3.新建一个提示上传文件错误消息的message.properties文件



4.写一个类UploadAction继承ActionSupport

package com.sve.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

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;
	}
	
	public String execute() throws IOException {
		for(int i=0;i<file.size();i++) {
			//保存文件的路径
			String dir = ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName.get(i));
			System.out.println(dir);
			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 "success";
	}
}


5.写一个upload.jsp页面  和一个success.jsp成功页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传多个文件</title>
</head>
<body>
	<s:fielderror name="fieldError"></s:fielderror>
	<s:form theme="simple" action="upload" enctype="multipart/form-data" method="post">
		file1:<s:file name="file"></s:file>
		<br/><br/>
		file2:<s:file name="file"></s:file>
		<br/><br/>
		file3:<s:file name="file"></s:file>
		<s:submit value="上传"></s:submit>
	</s:form>
</body>
</html>


6.配置struts.xml

	<constant name="struts.custom.i18n.resources" value="message"></constant>
	<package name="default" extends="struts-default">
		<interceptors>
			<interceptor-stack name="mystack">
				<interceptor-ref name="defaultStack">
					<param name="fileUpload.maximumSize">3000000</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="mystack"></default-interceptor-ref>
		<action name="upload" class="com.sve.action.UploadAction">
			<result>/success.jsp</result>
			<result name="input">/upload.jsp</result>
		</action>
	</package>
</struts>


7.在web-app新建一个upload文件夹

最后发布项目访问http://loalhost:8080/SIUploads/upload.jsp  选择文件点点击上传即可。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值