You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Part类型getSubmittedFileName方法未定义问题解决(Servlet4.0+Tomcat9)

问题

开发Java Web项目时遇到错误:The method getSubmittedFileName is undefined for the type Part。已导入javax.servlet.http.Part包,当前使用Servlet 4.0版本、Tomcat 9.0.82版本,项目为Maven工程且配置了相关依赖,但问题仍未解决。

代码片段

package com.admin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


@WebServlet("/add_books")
@MultipartConfig
public class BooksAdd  extends HttpServlet{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        try {
            
            String bookName=req.getParameter("bname");
            String author=req.getParameter("author");
            Double price=Double.parseDouble(req.getParameter("price"));
            String categories=req.getParameter("btype");
            String status=req.getParameter("bstatus");
            Part part=req.getPart("bimg");
            String fileName=part.getSubmittedFileName();
            
        
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
}

Maven依赖配置

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    
    <dependency>
        <groupId>jakarta.servlet.jsp.jstl</groupId>
        <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
        <version>2.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>jakarta.servlet.jsp.jstl</artifactId>
        <version>2.0.0</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>

</dependencies>

解决方案

  • 添加Servlet API编译依赖:你的Maven依赖中缺少Servlet API的依赖,编译时无法找到getSubmittedFileName()方法的定义。添加以下依赖到pom.xml中,注意设置scope为provided,避免和Tomcat自带的Servlet API冲突:
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
  • 清理并重新构建项目:执行Maven命令mvn clean compile,确保新依赖被正确导入,清除旧的编译缓存。
  • 手动解析文件名(备用方案):如果添加依赖后仍有问题,可以通过解析Part的Content-Disposition头来获取文件名,代码示例:
    String fileName = null;
    String contentDisposition = part.getHeader("Content-Disposition");
    if (contentDisposition != null) {
        int startIndex = contentDisposition.indexOf("filename=\"");
        int endIndex = contentDisposition.lastIndexOf("\"");
        if (startIndex != -1 && endIndex != -1) {
            fileName = contentDisposition.substring(startIndex + 10, endIndex);
        }
    }
    

内容的提问来源于stack exchange,提问作者Dinanath Salunke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:26:08