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