The method getSubmittedFileName() is undefined for the type Part

与世无争的帅哥 提交于 2019-12-06 06:04:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!