问题
I am trying to upload multiple files in servlet 3.0> .So this is my code.I am geting an error at getSubmittedFileName() method. Dont know why.Help!!
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)
}
回答1:
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:
- getSubmittedFileName of Part Interface
- Tomcat Versions
回答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.
来源:https://stackoverflow.com/questions/41038886/the-method-getsubmittedfilename-is-undefined-for-the-type-part