Struts2上传文件通过配置文件实现文件过滤

struts.xml实现过滤,使用拦截器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">	
<struts>
	<!-- 指定国际化资源文件的baseName为globalMessages -->
	<constant name="struts.custom.i18n.resources"
		value="globalMessages"/>
	<!-- 设置该应用使用的解码集 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<package name="lee" extends="struts-default">
		<!-- 配置处理文件上传的Action -->
		<action name="upload" class="lee.UploadAction">
			<!-- 配置fileUpload的拦截器 -->
			<interceptor-ref name="fileUpload">
				<!-- 配置允许上传的文件类型 -->
				<param name="allowedTypes">image/bmp,image/png,
					image/gif,image/jpeg</param>
				<!-- 配置允许上传的文件大小 -->
				<param name="maximumSize">2000000</param> 
			</interceptor-ref> 
			<!-- 配置系统默认的拦截器 -->
			<interceptor-ref name="defaultStack"/>
			<!-- 动态设置Action的属性值 
			<param name="savePath">/upload</param>-->
			<!-- 配置input逻辑视图对应的视图页面 -->
			<result name="input">/upload.jsp</result>
			<!-- 配置Struts 2默认的视图页面 -->
			<result>/succ.jsp</result>	
		</action>
	</package>
</struts>

客户端端jsp请求
<%@ page contentType="text/html; charset=GBK" language="java"
	errorPage=""%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>使用List上传多个文件</title>
		<meta name="website" content="http://www.crazyit.org" />
	</head>
	<body>
		<span style="color: red"><s:fielderror />
		</span>
		<form action="upload.action" method="post"
			enctype="multipart/form-data">
			文件标题:
			<input type="text" name="title" />
			<br />
			选择第一个文件:
			<input type="file" name="upload" />
			<br />
			选择第二个文件:
			<input type="file" name="upload" />
			<br />
			选择第三个文件:
			<input type="file" name="upload" />
			<br />
			<input value="上传" type="submit" />
		</form>
	</body>
</html>


Action处理

package lee;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport implements 
ServletContextAware {
	private String title;
	//使用File数组封装多个文件域对应的文件内容
	private List<File> upload;
	//使用字符串数组封装多个文件域对应的文件类型
	private List<String> uploadContentType;
	//使用字符串数组封装多个文件域对应的文件名字
	private List<String> uploadFileName;
	
	private ServletContext content;
	
	@Override
	public String execute() throws Exception
	{
		//取得需要上传的文件数组
		List<File> files = getUpload();
		//遍历每个需要上传的文件
		for (int i = 0 ; i < files.size() ; i++)
		{
			//以服务器的文件保存地址和原文件名建立上传文件输出流
			FileOutputStream fos = new FileOutputStream(content.getRealPath("/upload")
					 + "\\" + getUploadFileName().get(i));
			//以每个需要上传的文件建立文件输入流
			FileInputStream fis = new FileInputStream(files.get(i));
			//将每个需要上传的文件写到服务器对应的文件中
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
			{
				fos.write(buffer , 0 , len);
			}
			fos.close();
		}
		return SUCCESS;
    }
	
	//title属性的setter和getter方法
	public void setTitle(String title)
	{
		this.title = title;
	}
	public String getTitle()
	{
		return this.title;
	}
	//upload属性的setter和getter方法
	public void setUpload(List<File> upload)
	{
		this.upload = upload;
	}
	public List<File> getUpload()
	{
		return this.upload;
	}
	//uploadContentType属性的setter和getter方法
	public void setUploadContentType(List<String> uploadContentType)
	{
		this.uploadContentType = uploadContentType;
	}
	public List<String> getUploadContentType()
	{
		return this.uploadContentType;
	}
	//uploadFileName属性的setter和getter方法
	public void setUploadFileName(List<String> uploadFileName)
	{
		this.uploadFileName = uploadFileName;
	}
	public List<String> getUploadFileName()
	{
		return this.uploadFileName;
	}
	public ServletContext getContent() {
		return content;
	}
	public void setContent(ServletContext content) {
		this.content = content;
	}
	public void setServletContext(ServletContext content) {
		this.content = content;
	}
}


配置程序中出错的键值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值