William Miranda
William Miranda

Reputation: 603

Struts2 Upload File, validating size and content type file before send file

I have a problem with Struts 2 and its file upload interceptor. It is have validated my content type and my file size, but the upload process dont stop. Example my upload limit is 2Mb and user send a file with 500MB, the interceptor struts 2 log that file is too larger, but continue upload file

I'd like to stop the upload process, in error case, beacuse the struts2 have returned to my page only when the sending file is finished.

Thanks

Upvotes: 5

Views: 4617

Answers (2)

HariKrishna
HariKrishna

Reputation: 59

You can. Try this. It 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 restrict the total uploaded content size (not per file) add this at the top of your struts.xml file

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

Upvotes: -1

user497087
user497087

Reputation: 1591

You can't in Struts - since Struts doesn't get hold of the response object until your servlet container has finished processing it.

However - most containers have a means of controlling upload file sizes and, for tomcat, this is the maxPostSize parameter (default is 2GB) - as far as I can tell tomcat will count the number of incoming bytes and then exit if this value is exceeded. i.e Tomcat will not continue reading past maxPostSize. Other containers will have similair configuration parameters. I've never used it so I can't be of any further help here.

If you have nested containers/serves (for example, tomcat sitting behind Apache Web Server) make sure you configure the right one

Upvotes: 3

Related Questions