如何使用 FileUploadInterceptor 限制上传图片最大 2 MB 并返回自定义错误消息?

解读

国内项目普遍把文件上传安全作为硬性指标,面试官问“2 MB 限制 + 自定义错误提示”并不是只想听“在 struts.xml 里写个常量”,而是想确认你:

  1. 知道 FileUploadInterceptor 在拦截器栈中的执行顺序 与 OGNL 值栈回显机制;
  2. 能把 全局 i18n、字段级错误 与 Struts 声明式验证 串起来,给出可落地的“中文友好”方案;
  3. 明白 Struts 2.5+ 强制文件上传白名单 的新特性,避免现场答出过时配置。

一句话:既要“拦得住”,又要“说得清”,还要“报错得体”。

知识点

  1. FileUploadInterceptor 核心参数

    • maximumSize:以 Byte 为单位,2 MB = 2 * 1024 * 1024 = 2 097 152
    • allowedExtensions:白名单,如 jpg,jpeg,png,gif
    • allowedTypes:MIME 白名单,如 image/jpeg,image/png
  2. 覆盖默认错误键
    struts.messages.error.file.too.large → 文件总体积超限
    struts.messages.error.content.type.not.allowed → MIME 不匹配
    struts.messages.error.file.extension.not.allowed → 扩展名不匹配

  3. 中文资源文件位置
    放在 src/main/resources 下,命名 message_zh_CN.properties,并在 struts.xml 中通过 <constant name="struts.custom.i18n.resources" value="message"/> 加载。

  4. Action 内字段命名约定
    文件用 File file;文件名用 String fileFileName;文件类型用 String fileContentType;三者缺一不可,否则 OGNL 无法回填值栈。

  5. Struts 2.5+ 安全加固
    若出现 “FileUploadInterceptor 拒绝服务” 异常,需额外配置
    <constant name="struts.multipart.allowedExtensions" value="jpg,jpeg,png,gif"/>
    否则即使拦截器放行,Dispatcher 也会硬拦截。

答案

步骤一:在 src/main/resources/message_zh_CN.properties 写入自定义键值
struts.messages.error.file.too.large=上传图片不能超过 2MB,请重新选择!
struts.messages.error.content.type.not.allowed=仅支持 JPG/PNG/GIF 格式图片!
struts.messages.error.file.extension.not.allowed=文件扩展名必须是 jpg、jpeg、png 或 gif!

步骤二:struts.xml 开启资源文件并配置拦截器参数
<constant name="struts.custom.i18n.resources" value="message"/>
<constant name="struts.multipart.maxSize" value="2097152"/>
<constant name="struts.multipart.allowedExtensions" value="jpg,jpeg,png,gif"/>

<package name="upload" extends="struts-default"> <action name="uploadImage" class="cn.demo.action.ImageUploadAction"> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">2097152</param> <param name="fileUpload.allowedExtensions">jpg,jpeg,png,gif</param> <param name="fileUpload.allowedTypes">image/jpeg,image/png,image/gif</param> </interceptor-ref> <result name="input">/WEB-INF/jsp/upload.jsp</result> <result name="success">/WEB-INF/jsp/success.jsp</result> </action> </package>

步骤三:Action 代码骨架
public class ImageUploadAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;
// getter/setter 省略
public String execute(){
// 业务保存逻辑
return SUCCESS;
}
}

步骤四:JSP 回显错误
<s:fielderror fieldName="file"/>
<s:actionerror/>
<s:form action="uploadImage" enctype="multipart/form-data">
<s:file name="file" label="选择图片"/>
<s:submit value="上传"/>
</s:form>

当用户上传 5 MB 的 PSD 文件时,页面将直接显示 “上传图片不能超过 2MB,请重新选择!”,且 Action 不会进入 execute(),实现“限制 + 自定义提示”双重要求。

拓展思考

  1. 如果需求改成 “单张 2 MB,但一次请求可传 3 张,总大小不超过 5 MB”,需要 重写 FileUploadInterceptor,在 doIntercept 里循环累加 MultiPartRequestWrapper 的 File 数组长度,并 手动 addFieldError;同时把 maximumSize 设为 5 MB,但单文件校验逻辑下沉到自定义拦截器,避免官方拦截器“一刀切”。

  2. 在 Spring Boot + Struts 2 全注解 场景下,可用 @InterceptorRef 直接标注在 Action 类上,参数通过 @InterceptorRef(value="fileUpload", params={"maximumSize","2097152"}) 注入,无需 XML,但 struts.multipart.allowedExtensions 常量仍需在 application.yml 里声明,否则 2.5+ 会抛出 “Request exceeded size limit” 的 Dispatcher 级硬拦截。

  3. 高并发下大文件上传会 先写入 NIO 临时目录,建议 配合 nginx upload module 做 “秒传 + 切片”,让 Struts 只处理 已落盘的文件路径,从而 绕开 JVM 堆内存压力,这也是国内互联网大厂常用的 “前后端分离上传” 方案,面试时如能提及,可体现 架构视野。

牢记:“限制大小”只是入口,真正的加分项是“安全、性能、体验”三位一体。