Java File Upload to a Folder10 May 2025 | 5 min read The File plays a vital role in each programming language. In order to work with File, every developer should have knowledge of how we can implement the file upload functionality in Java or other programming languages. In this section, we will learn about how we can implement the file upload functionality in Java and will also understand how to upload a file to a folder. In Java, we use a single servlet and an HTML form for creating a file upload request to the servlet. We implement a Simple HTML form having two fields, i.e., File and Destination. The input field of type File allows the user to browse the File from the system. After selecting the File from the system, we send the File as a POST request to the server. We add two restrictions to the form, i.e., encrypt="multipart/form-data" and method="POST". With this restriction, the request sends to the server in the encoded form. The servlet handles the POST request, process the incoming file data, and extract File from the stream. The destination defines the path of the location where we need to save the File in the system. The data is posted to the servlet, and it stores the File to the specified location when we click on the Upload button. The code of the HTML form is as follows: index.html Output: ![]() POST and GET are the two methods that we can use at the time of submitting the form. Here, when we submit the form, the browser streams the content by combining all the parts. Each part defines the field of a form. In the above code, we use two annotations, i.e., @WebServlet having properties name and urlPatterns, and @MultipartConfig. The @WebServlet and the @MultipartConfig annotation are used for defining servlet mappings and for indicating that the servlet expects those requests which are made using the multipart/form-data MIME type, respectively. The servlet performs the following steps to store file data into the destination folder:
FileUploadServletExample.java In order to build, package, deploy and run the file upload program, we can either use NetBeans, Eclipse, or Ant. For NetBeans, we use the following steps:
Now, we navigate to the browser and enter the following URL: http://localhost:8080/fileupload/ After entering the URL and hit the enter key, the HTML form will open, which we create above. We browse the system and select a file to upload it to the desired location. In the destination field, we enter the path of the directory where we need to store that File. The directory should already be created in the system and must be writable. If we set nothing to the destination field and click on the upload button, it will throw an exception. When we click on the upload button, a POST request send to the servlet, and it will copy the File to the destination folder. After click on the upload button, we go to the specified directory and verify whether the uploaded File is present or not. ![]() |
Given an integer array “arr” and an integer k. We have an empty stack and the two following operations: “Push”, and “Pop”. We also have a stream of integers in the interval [1, k]. Use the two stack procedures to bring the numbers in the stack...
16 min read
? Creating a table in Java typically involves using data structures like arrays or collections. There are a few different ways to create a table in Java. One way is to use the JTable class. In this section, we will discuss the various ways to create table...
13 min read
In the world of Java programming, developers often encounter scenarios where they need to determine the status of a thread. Understanding whether a thread is alive or has completed its execution is crucial for efficient thread management. In such situations, the isAlive() method comes to the...
4 min read
Java is a powerful object-oriented programming language that provides developers with a wide range of tools and features to build robust and scalable applications. One of the features that make Java stand out is its support for generics. Generics allow developers to write generic classes and...
4 min read
MD5 is a cryptographic algorithm that provides the hash functions to get a fixed length 128-bit (16 bytes) hash value. Using Java, we can implement the MD5 hash in an application by using the MessageDigest class which is defined in java.security package. The Java MessageDigest class...
3 min read
The is a primitive data type. It is a single-precision 32-bit IEEE 754 floating point. It is used to declare the variables and methods. It represents the fractional numbers. Points to remember The float covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Its default value...
2 min read
In a binary tree, the greatest difference between a node and its ancestor is the highest value achieved by subtracting the value of a descendant node from one of its ancestor nodes. An ancestor of a node is any node located along the route from the...
5 min read
LU Decomposition LU decomposition, also called LU factorization. It is a fundamental technique in numerical linear algebra where a square matrix is split into a lower triangular matrix L and an upper triangular matrix U. For an n×n matrix A, the goal is to express it as the...
11 min read
Top 15 Frameworks are written in a programming language. In Java, we utilize frameworks to create applications using the Java programming language. The reusable code written in Java, which serves as a template, is referred to as a Java Framework. We develop apps by integrating...
7 min read
Spiral patterns are a popular concept in computer graphics and can be used to visualize data in a unique and interesting way. In this section, we will explore how to create a spiral pattern of numbers using Java. We will cover the logic behind the...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India