0

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?

user14773854
  • 303
  • 4
  • 10
  • https://stackoverflow.com/questions/37922973/spring-mvc-file-upload-unable-to-process-parts-as-no-multi-part-configuration Might help – seenukarthi Jul 12 '21 at 10:56
  • 1
    Does this answer your question? [Spring MVC file upload - Unable to process parts as no multi-part configuration has been provided](https://stackoverflow.com/questions/37922973/spring-mvc-file-upload-unable-to-process-parts-as-no-multi-part-configuration) – Joe Jul 12 '21 at 11:00
  • 1
    @Joe Nope. Still throws exceptions even after modifying web.xml. – user14773854 Jul 12 '21 at 11:06

1 Answers1

0

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>
acrastt
  • 104
  • 1
  • 7