javafile-uploadservlet-3.0

The method getSubmittedFileName() is undefined for the type Part


I am trying to upload multiple files in servlet 3.0>. I am getting an error at getSubmittedFileName() method. Why am I getting this error?

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String description = request.getParameter("description"); // Retrieves
                                                                // <input
                                                                // type="text"
                                                                // name="description">
    Part filePart = request.getPart("file"); // Retrieves <input type="file"
                                                // name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName())
            .getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();
    // ... (do your job here)
}

Solution

  • I got this problem before. Some guy helped me to find the root of this issue, so here is the solution I got:

    In the DOC of Java EE 7 you can see that the 'Interface Part' has been added to it getSubmittedFileName method since Servlet 3.1, and from tomcat website you can see that Tomcat 7 implemented Servlet 3.0, so I needed to upgrade from Tomcat 7 to Tomcat 8.0.x.

    References: