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:)
Remove this part
@MultipartConfig(
maxFileSize=1024*1024 // 1Mb max
)
and retry. By default MultipartRequest is limiting the upload size to 1 Megabyte
User contributions licensed under CC BY-SA 3.0