struts2之文件上传

这篇博客探讨了Struts2中文件上传的实现,重点关注FileUploadInterceptor拦截器的作用。文章介绍了Action类的设计,强调了文件参数名的命名规则,并提到了struts.xml配置文件的正确顺序。在实际操作中,当上传文件与拦截器配置不匹配时,系统会返回错误信息。此外,还提及了如何支持同时上传多个文件的方法。
摘要由CSDN通过智能技术生成

struts2对文件的上传进行了封装,主要核心是FileUploadInterceptor拦截器对文件上传请求的操作,那么对于文件上传的实现,我们开发或者需要注意哪些呢?

我们先来大概解析一下FileUploadInterceptor拦截器对应的javadoc文档:

Open Declaration org.apache.struts2.interceptor.FileUploadInterceptor
/*
 * FileUploadInterceptor拦截器是基于MultiPartRequestWrapper,将会拦截所有包含文件的请求
 * 以下是所要求参数格式
 */
Interceptor that is based off of MultiPartRequestWrapper, which is automatically applied for any request that 
includes a file. It adds the following parameters, where [File Name] is the name given to the file uploaded 
by the HTML form: 
//所要上传的文件(自定义名字)
[File Name] : File - the actual File 
//所上传文件的内容类型
[File Name]ContentType : String - the content type of the file 
//所上传文件的真实文件名
[File Name]FileName : String - the actual name of the file uploaded (not the HTML name) 

/*
 * 可以在Action类中提供setter方法获取这些文件
 */
You can get access to these files by merely providing setters in your action that correspond to any of the 
three patterns above, such as setDocument(File document), setDocumentContentType(String contentType), etc. 
See the example code section. 

/*
 * 如果Action类实现了ValidationAware,FileUploadInterceptor拦截器将会加入错误消息,这些消息是默认的
 * 国际化消息,可以通过以下方式覆盖
 */
This interceptor will add several field errors, assuming that the action implements ValidationAware. These 
error messages are based on several i18n values stored in struts-messages.properties, a default i18n file 
processed for all i18n requests. You can override the text of these messages by providing text for the 
following keys: 
//通常文件上传出错的消息
struts.messages.error.uploading - a general error that occurs when the file could not be uploaded 
//上传文件超过最大值
struts.messages.error.file.too.large - occurs when the uploaded file is too large 
//上传的文件内容不合法
struts.messages.error.content.type.not.allowed - occurs when the uploaded file does not match the expected 
content types specified 
//上传文件的扩展名不合法
struts.messages.error.file.extension.not.all
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值