3

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)
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Debasish Ghosh
  • 1,867
  • 20
  • 29

4 Answers4

2

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:

Ahmed Shendy
  • 1,424
  • 15
  • 28
2

If you're using Servlet 3.0, you will have to define the getSubmittedFileName() manually.

Scroll down in this answer to "When you're not on Servlet 3.1 yet, manually get submitted file name" for the method definition and change it according to your needs.

Mohit Dodhia
  • 333
  • 4
  • 15
2

i have gone through this problem, simply apply this dependancy in maven pom.xml file...

     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
     <dependency>
         <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
       </dependency>
0

If somebody like me came here having getSubmittedFileName() null in a

for (Part filePart : request.getParts()) loop

then just know that not every Part has a FileName property :)