This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch fix/file-upload-navigation in repository https://gitbox.apache.org/repos/asf/struts-site.git
commit 0fccbce237ab3d2b4da12ea666d8c027373b86e8 Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Sat Mar 16 16:34:44 2024 +0100 Splits file upload page --- .../action-file-upload-interceptor.md | 2 +- .../{file-upload.md => action-file-upload.md} | 199 ++------------------- source/core-developers/file-upload.md | 52 ++---- 3 files changed, 24 insertions(+), 229 deletions(-) diff --git a/source/core-developers/action-file-upload-interceptor.md b/source/core-developers/action-file-upload-interceptor.md index 6019347ef..1c8902407 100644 --- a/source/core-developers/action-file-upload-interceptor.md +++ b/source/core-developers/action-file-upload-interceptor.md @@ -10,7 +10,7 @@ parent: > Available since Struts 6.4.0 as replacement for [File Upload > Interceptor](file-upload-interceptor) -See [this page](file-upload) for more examples and advanced configuration. +See [this page](action-file-upload) for more examples and advanced configuration. Interceptor that is based off of `MultiPartRequestWrapper`, which is automatically applied for any request that includes a file. If an action implements `org.apache.struts2.action.UploadedFilesAware` interface, the interceptor will pass diff --git a/source/core-developers/file-upload.md b/source/core-developers/action-file-upload.md similarity index 67% copy from source/core-developers/file-upload.md copy to source/core-developers/action-file-upload.md index 054464f29..94f93950d 100644 --- a/source/core-developers/file-upload.md +++ b/source/core-developers/action-file-upload.md @@ -1,12 +1,12 @@ --- layout: default -title: File Upload +title: Action File Upload parent: title: Action File Upload Interceptor url: action-file-upload-interceptor --- -# File Upload +# Action File Upload {:.no_toc} * Will be replaced with the ToC, excluding a header @@ -24,14 +24,11 @@ than the temporary directory and the directories that belong to your web applica The Struts 2 framework leverages the Commons FileUpload library as a based library to support file upload in the framework. The library is included in a base Struts 2 distribution. -> NOTE: Since Struts 6.4.0 the `FileUploadInterceptor` is deprecated, and you should use `ActionFileUploadInterceptor` instead! - ## Basic Usage -The `org.apache.struts2.interceptor.FileUploadInterceptor` and `org.apache.struts2.interceptor.ActionFileUploadInterceptor` -interceptors are included as part of the `defaultStack` and named appropriately: `fileUpload` and `actionFileUpload`. -As long as the required libraries are added to your project you will be able to take advantage of the Struts 2 file upload -capability. Configure an Action mapping for your Action class as you typically would. +The `org.apache.struts2.interceptor.ActionFileUploadInterceptor` interceptor is included as part of the `defaultStack` +and named `actionFileUpload`. As long as the required libraries are added to your project you will be able to take +advantage of the Struts 2 file upload capability. Configure an Action mapping for your Action class as you typically would. ### Example action mapping: @@ -84,55 +81,6 @@ public class UploadAction extends ActionSupport implements UploadedFilesAware { } ``` -**Deprecated approach**: the fileUpload interceptor will use setter injection to insert the uploaded file and related data into your Action -class. For a form field named `upload` you would provide the three setter methods shown in the following example: - -### Example Action class: - -```java -package com.example; - -import java.io.File; - -import com.opensymphony.xwork2.ActionSupport; - -public class UploadAction extends ActionSupport { - private File file; - private String contentType; - private String filename; - - @StrutsParameter - public void setUpload(File file) { - this.file = file; - } - - @StrutsParameter - public void setUploadContentType(String contentType) { - this.contentType = contentType; - } - - @StrutsParameter - public void setUploadFileName(String filename) { - this.filename = filename; - } - - public String execute() { - //... - return SUCCESS; - } -} -``` - -The purpose of each one of these methods is described in the table below. Notice that if you have multiple file form -elements with different names you would be required to have another corresponding set of these methods for each file -uploaded. - -| Method Signature | Description | -|---------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------| -| `setX(File file)` | The file that contains the content of the uploaded file. This is a temporary file and file.getName() will not return the original name of the file | -| `setXContentType(String contentType)` | The mime type of the uploaded file | -| `setXFileName(String fileName)` | The actual file name of the uploaded file (not the HTML name) | - ## Uploading Multiple Files As mentioned in the previous section one technique for uploading multiple files would be to simply have multiple form @@ -155,131 +103,8 @@ see `struts-fileupload.xml` in the sample application download. </s:form> ``` -The `org.apache.struts2.action.UploadedFilesAware` interface already supports uploading multiple files, you do not need to -follow the below example. - -**Deprecated approach**: `MultipleFileUploadUsingArrayAction.java` - -```java -public class MultipleFileUploadUsingArrayAction extends ActionSupport { - private File[] uploads; - private String[] uploadFileNames; - private String[] uploadContentTypes; - - public String upload() throws Exception { - System.out.println("\n\n upload2"); - System.out.println("files:"); - for (File u : uploads) { - System.out.println("*** " + u + "\t" + u.length()); - } - System.out.println("filenames:"); - for (String n : uploadFileNames) { - System.out.println("*** " + n); - } - System.out.println("content types:"); - for (String c : uploadContentTypes) { - System.out.println("*** " + c); - } - System.out.println("\n\n"); - return SUCCESS; - } - - public File[] getUpload() { - return this.uploads; - } - - @StrutsParameter - public void setUpload(File[] upload) { - this.uploads = upload; - } - - public String[] getUploadFileName() { - return this.uploadFileNames; - } - - @StrutsParameter - public void setUploadFileName(String[] uploadFileName) { - this.uploadFileNames = uploadFileName; - } - - public String[] getUploadContentType() { - return this.uploadContentTypes; - } - - @StrutsParameter - public void setUploadContentType(String[] uploadContentType) { - this.uploadContentTypes = uploadContentType; - } -} -``` - -### Uploading Multiple Files using Lists - -`multipleUploadUsingList.jsp` Notice all file input types have the same name. - -```html -<s:form action="doMultipleUploadUsingList" method="POST" enctype="multipart/form-data"> - <s:file label="File (1)" name="upload"/> - <s:file label="File (2)" name="upload"/> - <s:file label="FIle (3)" name="upload"/> - <s:submit cssClass="btn btn-primary"/> -</s:form> -``` - -`MultipleFileUploadUsingListAction.java` - -```java -public class MultipleFileUploadUsingListAction extends ActionSupport { - private List<File> uploads = new ArrayList<File>(); - private List<String> uploadFileNames = new ArrayList<String>(); - private List<String> uploadContentTypes = new ArrayList<String>(); - - public List<File> getUpload() { - return this.uploads; - } - - @StrutsParameter - public void setUpload(List<File> uploads) { - this.uploads = uploads; - } - - public List<String> getUploadFileName() { - return this.uploadFileNames; - } - - @StrutsParameter - public void setUploadFileName(List<String> uploadFileNames) { - this.uploadFileNames = uploadFileNames; - } - - public List<String> getUploadContentType() { - return this.uploadContentTypes; - } - - @StrutsParameter - public void setUploadContentType(List<String> contentTypes) { - this.uploadContentTypes = contentTypes; - } - - public String upload() throws Exception { - System.out.println("\n\n upload1"); - System.out.println("files:"); - for (File u : uploads) { - System.out.println("*** " + u + "\t" + u.length()); - } - System.out.println("filenames:"); - for (String n : uploadFileNames) { - System.out.println("*** " + n); - } - System.out.println("content types:"); - for (String c : uploadContentTypes) { - System.out.println("*** " + c); - } - System.out.println("\n\n"); - return SUCCESS; - } -} -``` +The `org.apache.struts2.action.UploadedFilesAware` interface already supports uploading multiple files, you do not need +to follow the below example. ## Advanced Configuration @@ -330,6 +155,7 @@ a file that is too large. Note that the aforementioned settings are applied at t precedence over this interceptor setting. Notice the locations of these settings in the following example: + ```xml <struts> <constant name="struts.multipart.maxSize" value="1000000"/> @@ -347,10 +173,11 @@ Notice the locations of these settings in the following example: </action> </struts> ``` + ### Normal Field Size Limit Since Struts 6.1.2.1 a new option has been introduced to limit the size of a normal string field in the multipart request. -The defeault limit is set to 4096 bytes: +The default limit is set to 4096 bytes: ``` struts.multipart.maxStringLength=4096 @@ -451,10 +278,8 @@ follow: ^multipart/form-data(?:\\s*;\\s*boundary=[0-9a-zA-Z'()+_,\\-./:=?]{1,70})?(?:\\s*;\\s*charset=[a-zA-Z\\-0-9]{3,14})? ``` -Please read [RFC1341](https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html) the **Multipart section** for more -details, -existing Struts `Multipart` parsers support only `multipart/form-data` content type. This option is available since -Struts 2.3.11. +Please read [RFC1341](https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html) the **Multipart section** for more details, existing Struts `Multipart` parsers support +only `multipart/form-data` content type. This option is available since Struts 2.3.11. ### Disabling file upload support diff --git a/source/core-developers/file-upload.md b/source/core-developers/file-upload.md index 054464f29..bcea91170 100644 --- a/source/core-developers/file-upload.md +++ b/source/core-developers/file-upload.md @@ -2,8 +2,8 @@ layout: default title: File Upload parent: - title: Action File Upload Interceptor - url: action-file-upload-interceptor + title: File Upload Interceptor + url: file-upload-interceptor --- # File Upload @@ -12,6 +12,9 @@ parent: * Will be replaced with the ToC, excluding a header {:toc} +> Since Struts 6.4.0 the `FileUploadInterceptor` is deprecated, and you should use [ActionFileUploadInterceptor](action-file-upload-interceptor) instead! +{:.alert .alert-block .alert-error} + The Struts 2 framework provides built-in support for processing file uploads that conform to [RFC 1867](http://www.ietf.org/rfc/rfc1867.txt), "Form-based File Upload in HTML". When correctly configured the framework will pass uploaded file(s) into your Action class. Support for individual and multiple file uploads are provided. When a file is uploaded it will typically be @@ -24,14 +27,11 @@ than the temporary directory and the directories that belong to your web applica The Struts 2 framework leverages the Commons FileUpload library as a based library to support file upload in the framework. The library is included in a base Struts 2 distribution. -> NOTE: Since Struts 6.4.0 the `FileUploadInterceptor` is deprecated, and you should use `ActionFileUploadInterceptor` instead! - ## Basic Usage -The `org.apache.struts2.interceptor.FileUploadInterceptor` and `org.apache.struts2.interceptor.ActionFileUploadInterceptor` -interceptors are included as part of the `defaultStack` and named appropriately: `fileUpload` and `actionFileUpload`. -As long as the required libraries are added to your project you will be able to take advantage of the Struts 2 file upload -capability. Configure an Action mapping for your Action class as you typically would. +The `org.apache.struts2.interceptor.FileUploadInterceptor` interceptor is included as part of the `defaultStack` +and named `fileUpload`. As long as the required libraries are added to your project you will be able to take +advantage of the Struts 2 file upload capability. Configure an Action mapping for your Action class as you typically would. ### Example action mapping: @@ -56,35 +56,7 @@ example: </s:form> ``` -The actionFileUpload interceptor will use a dedicated interface `org.apache.struts2.action.UploadedFilesAware` to transfer -information and content of uploaded file. Your action should implement the interface to receive the uploaded file: - -```java -public class UploadAction extends ActionSupport implements UploadedFilesAware { - - private UploadedFile uploadedFile; - private String contentType; - private String fileName; - private String originalName; - - @Override - public void withUploadedFiles(List<UploadedFile> uploadedFiles) { - if (!uploadedFiles.isEmpty()) { - this.uploadedFile = uploadedFiles.get(0); - this.fileName = uploadedFile.getName(); - this.contentType = uploadedFile.getContentType(); - this.originalName = uploadedFile.getOriginalName(); - } - } - - public String execute() { - // do something with the file - return SUCCESS; - } -} -``` - -**Deprecated approach**: the fileUpload interceptor will use setter injection to insert the uploaded file and related data into your Action +The **fileUpload** interceptor will use setter injection to insert the uploaded file and related data into your Action class. For a form field named `upload` you would provide the three setter methods shown in the following example: ### Example Action class: @@ -155,10 +127,7 @@ see `struts-fileupload.xml` in the sample application download. </s:form> ``` -The `org.apache.struts2.action.UploadedFilesAware` interface already supports uploading multiple files, you do not need to -follow the below example. - -**Deprecated approach**: `MultipleFileUploadUsingArrayAction.java` +**MultipleFileUploadUsingArrayAction.java** ```java public class MultipleFileUploadUsingArrayAction extends ActionSupport { @@ -347,6 +316,7 @@ Notice the locations of these settings in the following example: </action> </struts> ``` + ### Normal Field Size Limit Since Struts 6.1.2.1 a new option has been introduced to limit the size of a normal string field in the multipart request.