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 2023/12/11 06:56:47 UTC
(struts) 02/02: WW-5371 Uses the new upload mechanism in Showcase app
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch feature/WW-5371-modern-upload
in repository https://gitbox.apache.org/repos/asf/struts.git
commit dc4103bcb55fa56176bdf014a3f9551cf57fb350
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Mon Dec 11 07:56:38 2023 +0100
WW-5371 Uses the new upload mechanism in Showcase app
---
.../showcase/fileupload/FileUploadAction.java | 101 ++++++++++-----------
.../webapp/WEB-INF/fileupload/upload-success.jsp | 9 +-
.../dispatcher/multipart/StrutsUploadedFile.java | 8 ++
3 files changed, 61 insertions(+), 57 deletions(-)
diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
index ed8a11b48..246f8ad70 100644
--- a/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
+++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java
@@ -21,75 +21,70 @@
package org.apache.struts2.showcase.fileupload;
import com.opensymphony.xwork2.ActionSupport;
+import org.apache.struts2.action.UploadedFilesAware;
+import org.apache.struts2.dispatcher.multipart.UploadedFile;
import java.io.File;
+import java.util.List;
/**
* Show case File Upload example's action. <code>FileUploadAction</code>
*/
-public class FileUploadAction extends ActionSupport {
+public class FileUploadAction extends ActionSupport implements UploadedFilesAware {
- private static final long serialVersionUID = 5156288255337069381L;
+ private static final long serialVersionUID = 5156288255337069381L;
- private String contentType;
- private File upload;
- private String fileName;
- private String caption;
+ private String contentType;
+ private UploadedFile uploadedFile;
+ private String fileName;
+ private String caption;
+ private String originalName;
- public String input() throws Exception {
- return SUCCESS;
- }
+ public String input() throws Exception {
+ return SUCCESS;
+ }
- public String upload() throws Exception {
- return SUCCESS;
- }
+ public String upload() throws Exception {
+ return SUCCESS;
+ }
- // since we are using <s:file name="upload" .../> the file name will be
- // obtained through getter/setter of <file-tag-name>FileName
- public String getUploadFileName() {
- return fileName;
- }
+ public String getContentType() {
+ return contentType;
+ }
- public void setUploadFileName(String fileName) {
- this.fileName = fileName;
- }
+ public String getFileName() {
+ return fileName;
+ }
+ public String getOriginalName() {
+ return originalName;
+ }
- // since we are using <s:file name="upload" ... /> the content type will be
- // obtained through getter/setter of <file-tag-name>ContentType
- public String getUploadContentType() {
- return contentType;
- }
+ public Object getUploadedFile() {
+ return uploadedFile.getContent();
+ }
- public void setUploadContentType(String contentType) {
- this.contentType = contentType;
- }
+ public String getCaption() {
+ return caption;
+ }
+ public void setCaption(String caption) {
+ this.caption = caption;
+ }
- // since we are using <s:file name="upload" ... /> the File itself will be
- // obtained through getter/setter of <file-tag-name>
- public File getUpload() {
- return upload;
- }
-
- public void setUpload(File upload) {
- this.upload = upload;
- }
-
-
- public String getCaption() {
- return caption;
- }
-
- public void setCaption(String caption) {
- this.caption = caption;
- }
-
- public long getUploadSize() {
- if (upload != null) {
- return upload.length();
- } else {
- return 0;
- }
+ public long getUploadSize() {
+ if (uploadedFile != null) {
+ return uploadedFile.length();
+ } else {
+ return 0;
}
+ }
+
+ @Override
+ public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
+ this.uploadedFile = uploadedFiles.get(0);
+ this.fileName = uploadedFile.getName();
+ this.contentType = uploadedFile.getContentType();
+ this.originalName = uploadedFile.getOriginalName();
+ }
}
diff --git a/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp b/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
index d50b61be0..a1b06277a 100644
--- a/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
+++ b/apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
@@ -19,7 +19,7 @@
*/
-->
<%@ page
- language="java"
+ language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
@@ -37,9 +37,10 @@
<div class="row">
<div class="col-md-12">
<ul>
- <li>ContentType: <s:property value="uploadContentType" /></li>
- <li>FileName: <s:property value="uploadFileName" /></li>
- <li>File: <s:property value="upload" /></li>
+ <li>ContentType: <s:property value="contentType" /></li>
+ <li>FileName: <s:property value="fileName" /></li>
+ <li>Original FileName: <s:property value="originalName" /></li>
+ <li>File: <s:property value="uploadedFile" /></li>
<li>Caption:<s:property value="caption" /></li>
</ul>
</div>
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
index 97cb9cefd..55233d7d7 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java
@@ -79,6 +79,14 @@ public class StrutsUploadedFile implements UploadedFile {
return originalName;
}
+ @Override
+ public String toString() {
+ return "StrutsUploadedFile{" +
+ "contentType='" + contentType + '\'' +
+ ", originalName='" + originalName + '\'' +
+ '}';
+ }
+
public static class Builder {
private final File file;
private String contentType;