Struts2 Download a file by checking the size and type of contents of a file before sending a file

I have a problem with Struts 2 and its file upload interceptor. He confirmed my content type and my file size, but the download process does not stop. An example of my upload limit is 2 MB, and the user sends a file with 500 MB, the interceptor intercepts 2 logs, this file is too large, but continues to download the file

I would like to stop the download process, in case of an error, get struts2 to return to my page only after the file upload is complete.

thanks

+4
source share
2 answers

You cannot in Struts - because Struts does not receive a response object until your servlet container completes its processing.

However - most containers have controls over the size of the downloaded files, and for tomcat it is the maxPostSize parameter (default is 2 GB) - as far as I can tell, tomcat will count the number of bytes received, and then exit if this value is exceeded. Those. Tomcat will not continue to read past maxPostSize. Other containers will have similair configuration options. I have never used it, so I can no longer help.

If you have nested containers / services (e.g. tomcat sitting at the Apache Web Server), make sure you set it up correctly

+2
source

You can. Try it. He works.

<action name="uploadDocs" class="com.test.myapp.UploadAction" method="upload"> <interceptor-ref name="fileUpload"> <param name="maximumSize">2097152</param> <!-- Max 2 MB limit per file--> <param name="allowedTypes">image/jpeg,image/bmp</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <interceptor-ref name="execAndWait"/> <param name="delay">1000</param> <result name="wait">wait.jsp</result> <!-- display custom please wait page --> <result name="success">cmresulttemp.jsp</result> <result name="input">attachfiles.jsp</result> <result name="error">attachfiles.jsp</result> </action> 

If you want to limit the total size of the downloaded content (not for the file), add it to the top of struts.xml

 <constant name="struts.multipart.maxSize" value="10485760" /> <!-- Total 10 MB--> 
-1
source

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


All Articles