'Tomcat 10 throws error while uploading files jsp

I am using the example provided by @BalusC in the question. But I am getting an exception

java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
    org.apache.catalina.connector.Request.parseParts(Request.java:2824)
    org.apache.catalina.connector.Request.getParts(Request.java:2792)
    org.apache.catalina.connector.Request.getPart(Request.java:2961)
    org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1105)
    com.example.JSPtest.upload.doPost(upload.java:36)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:689)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:770)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

This is what I have done.

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="text" name="description" />
    <input type="file" name="file" />
    <input type="submit" />
</form>
static String URL = "localhost:3306/";
static String DATABASE_NAME = "DB";
static String USERNAME = "user";
static String PASSWORD = "";
@WebServlet("/upload")
@MultipartConfig(maxFileSize = 16*1024*1024)
public class UploadServlet extends HttpServlet {
    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)
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://" + URL + DATABASE_NAME, USERNAME, PASSWORD);
            PreparedStatement ps = con.prepareStatement("insert into data(image) values(?)");
            ps.setBinaryStream(1, fileContent);
            int result = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The data column is a blob. Note I have not used servlets as IntelliJ has an issue where it is not finding the java servlet class files.
Any help?



Solution 1:[1]

Add this to web.xml:

<multipart-config>
    <location>/tmp</location>
    <max-file-size>20848820</max-file-size>
    <max-request-size>418018841</max-request-size>
    <file-size-threshold>1048576</file-size-threshold>
</multipart-config>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 engine