How can I upload files using struts 2 working with large files?

I want to create a web application with a file upload mechanism using struts2 with fileUpload , which behaves strangely, and I cannot understand why.

I configured the file upload mechanism in struts.xml:

 <package name="com.actions" namespace="/" extends="struts-default"> <action name="excelupload" class="com.actions.FileuploadAction"> <interceptor-ref name="fileUpload"> <param name="maximumSize">10000000</param> </interceptor-ref> <result name="success">/fileupload.jsp</result> <result name="input">/fileupload.jsp</result> </action> </package> 

Corresponding JSP:

 <s:form action="excelupload" method="post" enctype="multipart/form-data"> <s:file name="excelfile" label="file" /> <s:submit name="upload" value="upload" align="center" /> </s:form> 

Corresponding action:

 public class FileuploadAction extends ActionSupport{ File excelfile; public File getExcelfile() { return excelfile; } public void setExcelfile(File excelfile) { this.excelfile = excelfile; } public String execute(){ System.out.println(excelfile.getName()); return SUCCESS; } } 

When I upload a small file, everything works fine. But when I try to upload a file larger than 2 MB, the application throws the following exception:

 org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3244109) exceeds the configured maximum (2097152) 

To fix this issue, I tried adding struts-default.properties with

 struts.multipart.maxSize=20097152 

to the root of my application class path.

An exception has been fixed, but now the excelfile always null , no matter how large the file is.

Does anyone know what I'm doing wrong?

EDIT . For deployment I use Tomcat EDIT : missing getter and setter methods are added.

+4
source share
1 answer

I could fix this problem by removing

 <interceptor-ref name="fileUpload"> <param name="maximumSize">10000000</param> </interceptor-ref> 

from struts.xml .

0
source

Source: https://habr.com/ru/post/1437394/


All Articles