0

Basically I have a type="file" which sends an image to server, In server I need to check that the image size is not higher than 1MB if it's send an error to client about the image size so the client could pop an error to user.

Here is my servlet script:

@MultipartConfig(
    maxFileSize=1024*1024     // 1Mb max
)
public class ProPicUploader extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException
{
    System.out.println("Initiate method is called in " + this.getClass().getName());
}
public void doPost(HttpServletRequest request, 
      HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    try{
        MultipartRequest multipartRequest = new MultipartRequest(request, "D:\\");
    } catch(IOException e){
        out.print("Image size is bigger than 1MB");
        System.out.println(e.getMessage());
    }
    out.print("Successfully Uploaded");
}
public void destroy(){
    System.out.println("Destory method is called in: " + this.getClass().getName());
}
}

client-side script:

if(formdata){
        $.ajax({
            url: '../propicuploader',
            type: 'POST',
            data: formdata,
            processData: false,
            contentType: false,
            success: function(data){
                alert(data);
                if(data == "0x00000035"){
                    $("#NotPictureerror_spn").text("File size is too big, Please select a file upto 2MB");
                }
            }
            error: function(data){
                alert(data);
            }
        });
    }

and my web.xml script:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Duck</display-name>
  <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ProPicUploader</servlet-name>
    <servlet-class>/ProPicUploader</servlet-class>
  </servlet>
</web-app>

So far it does if the Image size is lower than 1MB it says Successfully Uploaded but if the image size is higher than 1MB it pop nothing although I have inputed to send an error saying out.print("Image size is bigger than 1MB") but my client never gets to receive that.

I really appreciate if somebody tell me what am i doing wrong:)

darees
  • 17
  • 2
  • 8

1 Answers1

0

Remove this part

@MultipartConfig(
    maxFileSize=1024*1024     // 1Mb max
) 

and retry. By default MultipartRequest is limiting the upload size to 1 Megabyte

Hassen Bennour
  • 3,885
  • 2
  • 12
  • 20
  • First I am so sorry about I didn't replied soon, second Later i may need to change that 1MB to more, so That do not answer my question – darees Sep 11 '16 at 19:24
  • if you want to limit only the maxFileSize by `@MultipartConfig` and not the whole `MultipartRequest.maxPostSize` in this case your ProPicUploader servlet will never receive the http request if the posted file size is bigger than maxFileSize, for this you need to treat this error on ajax side by working with statusCode because depending on how the web container maps IllegalStateException to response and this may be empty with statusCode 500 but normally the alert is shown in your case, did you reads the browser console logs if you have JS errors – Hassen Bennour Sep 12 '16 at 14:09
  • Actually I am limiting `maxPostsize` too to 512kb, I didn't posted it here – darees Sep 12 '16 at 14:36