Hi Burton,
The issue is with a simple HTTP request the file object is null, see the red
log entry below. But if I use a simple jQuery ajax call, it works as expected
and I’m not sure why and how to rectify.
This was the simple code I used to upload the file, same set of interceptors,
same action, same method and it works:
jQuery.ajax({
type: 'POST',
url: $form.attr('action'),
cache: false,
contentType: false,
processData: false,
data: formData,
success: function (response) {
if (response.success) {
console.log(“success, file uploaded
successfully”);
} else {
console.log(“failure, file upload failed to
execute successfully”);
}
},
error: function (err) {
console.log("Error ajax submission : ", err);
}
});
The log files now include:
action.IndexAction (IndexAction.java:54) - Uploaded file :
/apache-tomcat-9.0.44_base/work/Catalina/localhost/caams/upload_eabd436d_0fdd_4ff7_a598_b9a19724d8dd_00000000.tmp
action.IndexAction (IndexAction.java:55) - Uploaded file name : Invoice -
1331-1.pdf
action.IndexAction (IndexAction.java:56) - Uploaded file type : application/pdf
action.IndexAction (IndexAction.java:57) - Uploaded file length : 0
whereas with an HTTP request the logs looked like this:
action.IndexAction (IndexAction.java:54) - Uploaded file : null
action.IndexAction (IndexAction.java:55) - Uploaded file name : null
action.IndexAction (IndexAction.java:56) - Uploaded file type : null
action.IndexAction (IndexAction.java:57) - Uploaded file length : 0
I’m trying to understand why the same action method works with the ajax
requests but not plain HTTP requests?
Finally, 6.3 doesn’t have the
org.apache.struts2.interceptor.ActionFileUploadInterceptor in the jar so I
can’t see how we could test?
Z.
On 1/3/2024, 1:30 am, "Burton Rhodes" <[email protected]
<mailto:[email protected]>> wrote:
I'm not exactly sure what isn't working for you, but have you tried
using the newer FilesUploadAware interface? More information can be
found at https://struts.apache.org/core-developers/file-upload.html
<https://struts.apache.org/core-developers/file-upload.html>
I might also recommend you set <constant name="struts.devMode"
value="true"/> to see if anything comes up in the logs.
Thanks,
Burton
------ Original Message ------
>From "Zoran Avtarovski" <[email protected]
><mailto:[email protected]>>
To "Struts Users Mailing List" <[email protected]
<mailto:[email protected]>>
Date 2/28/2024 4:27:48 PM
Subject Struts 6.3 Issue Uploading Files
>Hi Guys,
>
>
>
>We are unable to upload files to our first 6.3 application using HTTP
>requests, but the strange thing is they work with ajax requests. I suspect we
>are overlooking something in the config which is required in 6.3.
>
>
>
>We are using 6.3.0.2 running on Tomcat 9. The file object is null after the
>upload but we can see the upload is there.
>
>
>
>I can see the params interceptor finds the file and copies it to the temp
>directory, but I then can’t access the file in the action??? What’s really
>strange is if we use the same mechanism using a an ajax request with FormData
>it works as expected. That’s why we created a simple setup as outlined below
>to identify the root cause.
>
>
>
>Any help would really be appreciated.
>
>
>
>Here’s a snippet of the logs:
>
>multipart.JakartaMultiPartRequest (JakartaMultiPartRequest.java:103) - Found
>file item: [upload]
>
>multipart.JakartaMultiPartRequest (JakartaMultiPartRequest.java:114) - Item is
>a file upload
>
>
>
>When I try and access the file in my action:
>
>action.IndexAction (IndexAction.java:54) - Uploaded file : null
>
>action.IndexAction (IndexAction.java:55) - Uploaded file name : null
>
>action.IndexAction (IndexAction.java:56) - Uploaded file type : null
>
>action.IndexAction (IndexAction.java:57) - Uploaded file length : 0
>
>
>
>
>
>And then after the request has completed:
>
>multipart.JakartaMultiPartRequest (JakartaMultiPartRequest.java:361) -
>Removing file upload name=Invoice - 1331-1.pdf, StoreLocation=
>/apache-tomcat-9.0.44_base/work/Catalina/localhost/caams/upload_0569c990_d32c_4688_ad50_44db275ab0cc_00000000.tmp,
> size=465396 bytes, isFormField=false, FieldName=upload
>
>
>
>This is what my form looks like:
>
>
>
><s:form enctype="multipart/form-data" action="testUpload" method="post"
>theme="simple">
>
><s:file name="upload" key="file.to.upload" />
>
> <button type="submit" >Submit</button>
>
></s:form>
>
>
>
>My action has the following (BaseAction extends ActionSupport):
>
>
>
>public class IndexAction extends BaseAction {
>
>
>
> private File upload;
>
> private String uploadFileName;
>
> private String uploadContentType;
>
> private long uploadContentLength;
>
>
>
> public IndexAction() {
>
> }
>
>
>
> @Override
>
> public String execute() {
>
> return SUCCESS;
>
> }
>
>
>
> public String edit() {
>
> return INPUT;
>
> }
>
>
>
> public String uploadTest() {
>
> try {
>
> LOGGER.debug("Uploaded file : "+ upload);
>
> LOGGER.debug("Uploaded file name : "+ uploadFileName);
>
> LOGGER.debug("Uploaded file type : "+ uploadContentType);
>
> LOGGER.debug("Uploaded file length : "+ uploadContentLength);
>
>
>
> inputStream = new FileInputStream(upload);
>
>
>
> if (inputStream != null) {
>
> inputStream.close();
>
> }
>
>
>
> } catch (Exception e) {
>
> LOGGER.error("General . uploading error :", e);
>
> }
>
>
>
> return SUCCESS;
>
> }
>
>
>
> public File getUpload() {
>
> return upload;
>
> }
>
>
>
> public void setUpload(File upload) {
>
> this.upload = upload;
>
> }
>
>
>
> public String getUploadFileName() {
>
> return uploadFileName;
>
> }
>
>
>
> public void setUploadFileName(String uploadFileName) {
>
> this.uploadFileName = uploadFileName;
>
> }
>
>
>
> public String getUploadContentType() {
>
> return uploadContentType;
>
> }
>
>
>
> public void setUploadContentType(String uploadContentType) {
>
> this.uploadContentType = uploadContentType;
>
> }
>
>
>
> public long getUploadContentLength() {
>
> return uploadContentLength;
>
> }
>
>
>
> public void setUploadContentLength(long uploadContentLength) {
>
> this.uploadContentLength = uploadContentLength;
>
> }
>
>}
>
>
>
>My relevant struts.xml config:
>
>
>
> <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
>
> <constant name="struts.i18n.encoding" value="UTF-8"/>
>
> <constant name="struts.devMode" value="false"/>
>
> <constant name="struts.locale" value="en_AU"/>
>
> <constant name="struts.ui.theme" value="simple"/>
>
> <constant name="struts.ui.templateDir" value="template"/>
>
>
>
> <constant name="struts.i18n.search.defaultbundles.first" value="true"/>
>
> <constant name="struts.ui.escapeHtmlBody" value="true"/>
>
>
>
> <!-- Max file size of 15mb -->
>
> <constant name="struts.multipart.maxSize" value="45000000" />
>
> <constant name="struts.multipart.maxStringLength" value="45000000" />
>
>
>
> <package name="spareTest" extends="default">
>
> <default-interceptor-ref name="paramsPrepareParamsStack"/>
>
>
>
> <action name="testUpload" class="indexAction" method="uploadTest">
>
> <result name="input" >/WEB-INF/pages/main/formUpload.jsp</result>
>
> <result name="success" type="redirectAction">home</result>
>
> </action>
>
>
>
> <action name="testFile" class="indexAction" method="edit">
>
> <result name="input" >/WEB-INF/pages/main/formUpload.jsp</result>
>
> </action>
>
>
>
> </package>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
<mailto:[email protected]>
For additional commands, e-mail: [email protected]
<mailto:[email protected]>