Tomcat 10 Webapp 迁移 - servletFileUpload.parseRequest 错误

Tomcat 10 Webapp migration - servletFileUpload.parseRequest error

我正在将 Webapps 从 Tomcat 9 环境迁移到 Tomcat 10 环境。 我正在使用 Eclipse IDE.

下面是引发错误的部分代码:

import java.util.List;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
[...]
DiskFileItemFactory diskFileItemFactory = null;
ServletFileUpload   servletFileUpload   = null;
List<FileItem>      fileItemsList       = null;

    diskFileItemFactory = new DiskFileItemFactory();
    servletFileUpload   = new ServletFileUpload(diskFileItemFactory);
        
    fileItemsList = (List<FileItem>) servletFileUpload.parseRequest(request);

错误链接到最后一行。

我收到错误:

The project was not built since its build path is incomplete. Cannot find the class file for javax.servlet.http.HttpServletRequest. Fix the build path then try building this project
The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from required .class files

作为此迁移的一部分,我不得不将 Webapps Java 源代码中的 javax.servlet 替换为 jakarta.servlet,并替换 .jars javax.servlet.jsp.jstl-1.2.1.jarjavax.servlet.jsp.jstl-api-1.2.1.jarjakarta.servlet.jsp.jstl-2.0.0.jarjakarta.servlet.jsp.jstl-api-2.0.0.jar .jars.

你能帮我解决这些错误吗?

====== 编辑 ======

Piotr P. Karwasz 的回答解决了我的问题。

但是,我没有添加注释,这似乎对我不起作用,我在 Webapp web.xml 文件中为相关的 servlet 添加了 <multipart-config> 元素,如下所示:

  <servlet>
    <description></description>
    <display-name>FooServlet</display-name>
    <servlet-name>FooServlet</servlet-name>
    <servlet-class>com.foo.bar.FooServlet</servlet-class>    
    <multipart-config>
      <location>/tmp</location>
      <max-request-size>418018841</max-request-size>
      <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>    
  </servlet>
  <servlet-mapping>
    <servlet-name>FooServlet</servlet-name>
    <url-pattern>/do_foo_servlet</url-pattern>
  </servlet-mapping>

commons-fileupload library is not and probably will never be compatible with Servlet 5.0. Since Servlet 3.0 a similar functionality has been included into the standard (see section 3.2).

要将您的代码迁移到 Tomcat10,您需要:

  1. 从您的依赖项中删除 commons-fileupload

  2. @MultipartConfig,

    注释你的 servlet
  3. 将对 ServletFileUpload#parseRequest 的调用替换为 HttpServletRequest#getParts:

    final Collection<Parts> fileItemsList = request.getParts;
    
  4. 由于FileItem and Part的方法几乎相同,剩下的代码只需要稍作改动。