struts2实现文件过滤

本文介绍了两种在Struts2框架中实现文件上传过滤的方法:一种是通过自定义Action类手动进行文件类型的验证;另一种是利用Struts2自带的文件上传拦截器功能,通过配置允许上传的文件类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、手动实现文件过滤

1.uploadAction.java

 

//文件过滤属性,通过struts.xml文件配置allowtypes属性值
	private String allowtypes;
	
	
	public String getAllowtypes() {
		return allowtypes;
	}


	public void setAllowtypes(String allowtypes) {
		this.allowtypes = allowtypes;
	}

比较当前上传文件的格式和允许上传文件格式

//过滤文件

	public String filetypes(){
		
		String filetype=getUploadContentType();
		String[] types=getAllowtypes().split(",");
		for(String type:types){
			if(type.equals(filetype)){
				return "ok";
			}
		}
		return "error";
	}
	
	@Override
	public String execute() throws Exception {

		if(filetypes().equals("error")){
			this.addFieldError("uploadfileerror", "上传文件类型错误");
			return "error";
		}


 

2.struts.xml(在struts配置文件中allowTypes的属性值)

 

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="utf-8"/>
    <package name="parameter" namespace="/" extends="struts-default">

        <action name="upload1" class="com.ru.action.UploadAction">
        	<param name="savepath">/upload</param>
        	<!-- 文件上传格式 -->
        	<param name="allowtypes">image/gif,image/x-png,image/jpg,image/jpeg,application/vnd.openxmlformats-officedocument.wordprocessingml.document</param>
            <result name="sucess">/WEB-INF/jsp/sucess.jsp</result>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </action>      
    </package>
</struts>

二、struts2提供的过滤机制

struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="utf-8"/>
    <package name="parameter" namespace="/" extends="struts-default">

        <action name="upload1" class="com.ru.action.UploadAction">
        	<param name="savepath">/upload</param>
        	
        	
        	
        	
        	<!-- Struts2提供的文件上传拦截器-->
        	<interceptor-ref name="fileUpload">
        	<!-- 允许上传的文件格式 -->
        		<param name="allowedTypes">image/gif,image/x-png,image/jpg,image/jpeg,application/vnd.openxmlformats-officedocument.wordprocessingml.document</param>
        	<!-- 附件大小 -->	
        		<param name="maximumSize">2000000</param>
        	</interceptor-ref>
        	<!-- 默认拦截器 -->
        	<interceptor-ref name="defaultStack"></interceptor-ref>
        	<!-- 配置struts2的默认视图文件必须是"input" -->
        	<result name="input">/WEB-INF/jsp/error.jsp</result>
        	
        	
        	
        	
            <result name="sucess">/WEB-INF/jsp/sucess.jsp</result>
            
        </action>      
    </package>
</struts>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值