🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
@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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment