I am getting a strange behavior for multi-part form. I have a simple HTML multi-part form and I am using Apache commons library to extract the fields and files. However, for some reason the servlet code ServletFileUpload.isMultipartContent(request) is returning false. Below are the HTML and Servlet code. Can someone please let me know where am I going wrong?
This is the HTML file code.
<body>
<form method="post" action="http://localhost:8080/myapp/handler" enctype="multi-part/form-data">
<input type="text" name="exp_name">
<input type="file" name="exp_image_upload_0">
<br />
<button type="submit">Submit</button>
<button class="btn">Cancel</button>
</form>
</body>
This is the Servlet Code
/** Common method called by doGet and doPost methods **/
private void executeRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
System.out.println("Content Type : " + request.getContentType());
System.out.println("Name : " + request.getParameter("exp_name"));
if (isMultiPart)
System.out.println(">>>> IS MULTIPART");
else
System.out.println(">>>> IS NOT MULTIPART");
}
For this code, I am always getting "IS NOT MULTIPART" printed. I am sure there is something I am missing or doing wrong, but am not able to identify exactly what? Help please.
change your enctype attribute in form tag as per below:
enctype="multipart/form-data"