@MultipartConfig
annotation, which is used to handle file uploads in Java web applications. This annotation is part of the Jakarta Servlet API and provides an easy way to specify the configuration for file upload handling in servlets.Step 1: Add Servlet Dependency to pom.xml
First, we need to add the Jakarta Servlet dependency to our pom.xml
file. We are using version 6.1.0 in this example.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides.servlet.tutorial</groupId>
<artifactId>java-servlet-tutorial</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>java-servlet-tutorial Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Jakarta Servlet dependency -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>java-servlet-tutorial</finalName>
</build>
</project>
Step 2: Create a Servlet Using @MultipartConfig
Let's create a servlet that handles file uploads using the @MultipartConfig
annotation.
FileUploadServlet.java
package net.javaguides.servlet.tutorial.httpservlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
@WebServlet("/upload")
@MultipartConfig(
fileSizeThreshold = 1024 * 1024 * 1, // 1 MB
maxFileSize = 1024 * 1024 * 10, // 10 MB
maxRequestSize = 1024 * 1024 * 15 // 15 MB
)
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
for (Part part : request.getParts()) {
String fileName = part.getSubmittedFileName();
InputStream fileContent = part.getInputStream();
// Handle the file content here, such as saving to disk or processing
out.println("<p>File " + fileName + " uploaded successfully!</p>");
}
out.close();
}
}
In this example, the FileUploadServlet
class is annotated with @MultipartConfig
. The fileSizeThreshold
, maxFileSize
, and maxRequestSize
attributes are used to configure the file upload handling.
Commonly Used Attributes of @MultipartConfig
The @MultipartConfig
annotation provides several attributes for configuring file uploads:
location
: Specifies the directory location where files will be stored.fileSizeThreshold
: Specifies the size threshold after which the file will be written to disk.maxFileSize
: Specifies the maximum size allowed for uploaded files.maxRequestSize
: Specifies the maximum size allowed for multipart/form-data requests.
Additional Examples of @MultipartConfig
File Upload with Specific Location
@WebServlet("/upload")
@MultipartConfig(
location = "/tmp",
fileSizeThreshold = 1024 * 1024 * 1, // 1 MB
maxFileSize = 1024 * 1024 * 10, // 10 MB
maxRequestSize = 1024 * 1024 * 15 // 15 MB
)
public class FileUploadServlet extends HttpServlet {
// Servlet implementation
}
File Upload with Different Thresholds and Sizes
@WebServlet("/upload")
@MultipartConfig(
fileSizeThreshold = 1024 * 512, // 512 KB
maxFileSize = 1024 * 1024 * 5, // 5 MB
maxRequestSize = 1024 * 1024 * 10 // 10 MB
)
public class FileUploadServlet extends HttpServlet {
// Servlet implementation
}
Conclusion
In this blog post, we explored the @MultipartConfig
annotation, which is used to handle file uploads in Java web applications. We covered the basic usage and provided additional examples to illustrate how to configure file uploads with different attributes. The @MultipartConfig
annotation simplifies file upload handling by allowing configuration directly in the Java class.
References
Related Servlet Posts
- What is a Servlet in Java?
- Servlet Life Cycle
- Servlet Interface Example
- GenericServlet Class Example
- HttpServlet Class Example Tutorial
- HttpServlet doGet() Method Example
- HttpServlet doPost() Method Example
- @WebServlet Annotation Example
- @WebInitParam Annotation Example
- @WebListener Annotation Example
- @WebFilter Annotation Example
- @MultipartConfig Annotation Example
- How to Return a JSON Response from a Java Servlet
- Servlet Registration Form + JDBC + MySQL Database Example
- Login Form Servlet + JDBC + MySQL Example
- Servlet JDBC Eclipse Example Tutorial
- JSP Servlet JDBC MySQL CRUD Example Tutorial
- Servlet + JSP + JDBC + MySQL Example
- Registration Form using JSP + Servlet + JDBC + Mysql Example
- Login Form using JSP + Servlet + JDBC + MySQL Example
- JSP Servlet Hibernate CRUD Example
- JSP Servlet Hibernate Web Application
- Hibernate Registration Form Example with JSP, Servlet, MySQL
- Login Form using JSP + Servlet + Hibernate + MySQL Example
Comments
Post a Comment
Leave Comment