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)
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.
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
Interceptor parameters:
- maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this is not related to the various properties found in struts.properties. Default to approximately 2MB.
- allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.
Extending the interceptor:
You can extend this interceptor and override the acceptFile(java.io.File, java.lang.String, java.lang.String, com.opensymphony.xwork2.ValidationAware, java.util.Locale)
method to provide more control over which files are supported and which are not.
Example code:
<action name="doUpload" class="com.examples.UploadAction"> <interceptor-ref name="fileUpload"/> <interceptor-ref name="basicStack"/> <result name="success">good_result.ftl</result> </action>
And then you need to set encoding multipart/form-data
in the form where the user selects the file to upload.
<a:form action="doUpload" method="post" enctype="multipart/form-data"> <a:file name="upload" label="File"/> <a:submit/> </a:form>
And then in your action code you'll have access to the File object if you provide setters according to the naming convention documented in the start.
public com.examples.UploadAction implemements Action { private File file; private String contentType; private String filename; public void setUpload(File file) { this.file = file; } public void setUploadContentType(String contentType) { this.contentType = contentType; } public void setUploadFileName(String filename) { this.filename = filename; } ... }
相关推荐
本文将深入探讨Struts2中的FileUploadInterceptor,这是一个专门用于处理文件上传的拦截器。 首先,我们要理解Struts2拦截器的工作原理。拦截器是在Action执行前后插入的逻辑处理单元,它可以用来做一些预处理或后...
这篇博文和提供的FileUploadInterceptor.class文件可能详细讨论了如何在Struts2中实现文件上传。 在Struts2中,文件上传涉及到以下几个关键知识点: 1. **Action类的配置**:首先,你需要创建一个处理文件上传请求...
同时,Struts2提供了一个`FileUploadInterceptor`拦截器,用于处理文件上传的相关逻辑,包括验证文件大小。 1. **文件大小设置**: 在Struts2的配置文件(通常为struts.xml)中,我们可以定义一个全局或特定于...
private FileUploadInterceptor fileUploadInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(fileUploadInterceptor); } } ``` 在实际应用中,...
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> <!-- 其他拦截器 --> <!-- 更多拦截器 --> <result name="success">/success.jsp ...
通过深入分析Struts2框架在处理文件上传时所采取的方法和技术,我们可以看到它不仅有效地集成了现有的文件上传库,还通过一系列精心设计的组件(如JakartaMultiPartRequest和FileUploadInterceptor)极大地简化了...
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> <result name="success">/success.jsp ``` 这样,我们就完成了HTML5+Ajax文件上传到Struts2...
Struts2提供了一个名为`FileUploadInterceptor`的拦截器,它可以处理多文件上传,确保文件大小不超过限制,并且可以处理部分上传失败的情况。 接着,我们需要在JSP页面中创建一个Flash组件,如SWFUpload,这是一个...
<interceptor name="fileUploadInterceptor" class="org.apache.struts2.interceptor.FileUploadInterceptor"> <param name="maximumFileSize">10000000 ${getText('errors.too.large.file')} ...
在Struts2中,文件上传功能是通过Struts2的核心拦截器`FileUploadInterceptor`来实现的。 首先,我们需要理解Struts2文件上传的基本流程: 1. 用户在前端页面通过表单提交包含文件的请求。 2. Struts2拦截器`...
首先,要实现Struts2的文件上传,你需要在Struts2配置文件(通常为struts.xml或struts-default.xml)中启用`FileUploadInterceptor`。这可以通过在`package`元素中添加`interceptors`标签并包含`fileUpload`拦截器来...
在 Struts2 中,文件上传功能是通过特定的拦截器(`FileUploadInterceptor`)来实现的。以下是对标题和描述中所述知识点的详细解释: 1. **文件上传原理**: 文件上传的本质是客户端浏览器将本地文件以二进制流的...
- 在Struts 2中,文件上传主要依赖于`org.apache.struts2.components.File`组件和`org.apache.struts2.interceptor.FileUploadInterceptor`拦截器。 - 用户通过HTML表单提交包含`<input type="file">`的字段来选择...
在Struts2中,我们可以使用内置的`FileUploadInterceptor`拦截器来处理文件上传请求。 首先,我们需要在前端创建一个HTML表单,这个表单包含一个`<input type="file">`元素,允许用户选择要上传的文件。表单的`...
Struts2框架提供了一个名为`FileUploadInterceptor`的拦截器,专门处理文件上传请求。这个拦截器可以解析请求中的多部分数据,将文件内容保存到服务器上,并将相关信息(如文件名、大小等)传递给Action。 首先,你...
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> <!-- ... other interceptors ... --> <!-- ... actions ... --> ``` 这里设置了临时文件保存目录和...
在Struts2中,文件上传是通过`struts2-core`库中的`FileUploadInterceptor`拦截器实现的。首先,你需要在Action类中定义一个`java.io.File`或`org.apache.struts2.dispatcher.multipart.FileItem`类型的属性,用来...
Struts2提供了`org.apache.struts2.interceptor.FileUploadInterceptor`拦截器来处理文件上传。这个拦截器会自动解析请求中的multipart/form-data类型的数据,将文件内容转换为Action类中的属性。你需要在Action类中...
但是,Struts2也提供了`org.apache.struts2.interceptor.FileUploadInterceptor`拦截器,它可以更方便地处理文件上传。为了使用这个拦截器,我们需要在struts.xml配置文件中添加以下内容: ```xml ...
在Struts2中,你可以创建一个Action类来处理文件上传请求,该类需要继承自`org.apache.struts2.interceptor.FileUploadInterceptor`或使用`@ultipart`注解。在Action中,文件会被映射到一个字段,通常是一个`List...