You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2024/03/24 20:46:09 UTC
(struts-site) 01/01: Adds example with supporting multiple file uploads
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 bb53f2be57d8f6428f46e0b97b60ea83aadacd9d
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Sun Mar 17 17:44:44 2024 +0100
Adds example with supporting multiple file uploads
---
.../action-file-upload-interceptor.md | 2 +
source/core-developers/action-file-upload.md | 63 ++++++++++++++++++----
source/core-developers/file-upload-interceptor.md | 2 +
3 files changed, 56 insertions(+), 11 deletions(-)
diff --git a/source/core-developers/action-file-upload-interceptor.md b/source/core-developers/action-file-upload-interceptor.md
index 0096c07ae..aa3a89d73 100644
--- a/source/core-developers/action-file-upload-interceptor.md
+++ b/source/core-developers/action-file-upload-interceptor.md
@@ -51,6 +51,8 @@ and which are not.
## Examples
+See [this page](action-file-upload) for more examples and advanced configuration.
+
**Example action mapping:**
```xml
diff --git a/source/core-developers/action-file-upload.md b/source/core-developers/action-file-upload.md
index 94f93950d..511cd440c 100644
--- a/source/core-developers/action-file-upload.md
+++ b/source/core-developers/action-file-upload.md
@@ -53,8 +53,8 @@ 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:
+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 {
@@ -86,16 +86,13 @@ public class UploadAction extends ActionSupport implements UploadedFilesAware {
As mentioned in the previous section one technique for uploading multiple files would be to simply have multiple form
input elements of type file all with different names. This would require a number of setter methods that was equal to 3
times the number of files being uploaded. Another option is to use Arrays or java.util.Lists. The following examples are
-taken from the Showcase example application that is part sample applications you can download
-at [http://struts.apache.org/download.cgi](http://struts.apache.org/download.cgi). For the Action mapping details
-see `struts-fileupload.xml` in the sample application download.
+taken from the Showcase example application that is part sample applications you can download at [download](http://struts.apache.org/download.cgi).
+For the Action mapping details see `struts-fileupload.xml` in the sample application download.
-### Uploading Multiple Files using Arrays
-
-`multipleUploadUsingArray.jsp` Notice all file input types have the same name.
+`multipleUploadUsingList.jsp` Notice all file input types have the same name.
```html
-<s:form action="doMultipleUploadUsingArray" method="POST" enctype="multipart/form-data">
+<s:form action="multipleFileUploadUsingList" 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"/>
@@ -103,8 +100,52 @@ 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.
+The `org.apache.struts2.action.UploadedFilesAware` interface already supports uploading multiple files:
+
+```java
+public class MultipleFileUploadUsingListAction extends ActionSupport implements UploadedFilesAware {
+
+ private List<UploadedFile> uploads = new ArrayList<>();
+
+ public List<UploadedFile> getUpload() {
+ return this.uploads;
+ }
+
+ @Override
+ public void withUploadedFiles(List<UploadedFile> uploads) {
+ this.uploads = uploads;
+ }
+
+ private List<String> getUploadFileNames() {
+ return this.uploads.stream()
+ .map(UploadedFile::getOriginalName)
+ .collect(Collectors.toList());
+ }
+
+ private List<String> getUploadContentTypes() {
+ return this.uploads.stream()
+ .map(UploadedFile::getContentType)
+ .collect(Collectors.toList());
+ }
+
+ public String execute() throws Exception {
+ System.out.println("files:");
+ for (UploadedFile u : uploads) {
+ System.out.println("*** " + u + "\t" + u.length());
+ }
+ System.out.println("filenames:");
+ for (String n : getUploadFileNames()) {
+ System.out.println("*** " + n);
+ }
+ System.out.println("content types:");
+ for (String c : getUploadContentTypes()) {
+ System.out.println("*** " + c);
+ }
+ System.out.println("\n\n");
+ return SUCCESS;
+ }
+}
+```
## Advanced Configuration
diff --git a/source/core-developers/file-upload-interceptor.md b/source/core-developers/file-upload-interceptor.md
index da96ae136..fbc6c9c77 100644
--- a/source/core-developers/file-upload-interceptor.md
+++ b/source/core-developers/file-upload-interceptor.md
@@ -57,6 +57,8 @@ and which are not.
## Examples
+See [this page](file-upload) for more examples and advanced configuration.
+
**Example action mapping:**
```xml