简体   繁体   中英

Multi-part form not working

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.

如下更改表单标签中的enctype属性:

enctype="multipart/form-data"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM