Upload File to the Server Using Servlet in Java

Introduction

This article explains how to upload files to the server using a Java servlet. The NetBeans IDE is used to create this application.

File-Uploading to the server in Java

For the file-uploading app we require the following files:

  • HTML file
  • Servlet File
  • XML file
  • cos.jar file

1. The HTML file is needed to create an interface, to provide a choice of files for the user.

2. The Servlet file processes that file and uploads it to the server

3. The XML file provides information about the Servlet file to the server. It is used for mapping purposes.

4. "COS.JAR" is a jar file to provide access to use the MultipartRequest class. This class is used in the servlet to upload a file successfully.

The following is the procedure to create the File-Upload app

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java web" -> "web application" as shown below.

Java web

Step 3

Type your project name as "UploadFileApp" as in the following.

UploadFileApp

Step 4

Now choose your Java version and server wizard as shown below.

Server and java version wizard

Step 5

Delete your default "index.jsp" file and create a new "index.html" file and write the following code there.

index.html

<!DOCTYPE html>

<html>

    <head>

        <title>User-Authentication</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">

    </head>

    <body bgcolor="pink">

        <form action="ServletUpload" method="post" enctype="multipart/form-data">

            Select File:<input type="file" name="fname"/><br/>

            <input type="image" src="ImageUpload.png"/>

        </form>

    </body>

</html>

Note:

In this HTML file I passed an image instead of a "submit button". If you want an image instead of a button then you can use it otherwise you can use a simple button tag.

Step 6

Create a Servlet named  "ServletUpload" and write the following code for it.

ServletUpload.java

import java.io.*;

import javax.servlet.ServletException;

import javax.servlet.http.*;

import com.oreilly.servlet.MultipartRequest;

 

public class ServletUpload extends HttpServlet {

 

    @Override

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

 

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

 

        MultipartRequest m = new MultipartRequest(request, "e:/file");

        out.print("File Successfully Uploaded");

    }

}

Note

  • In this servlet I used a jar file named "cos.jar". You can dowload it and upload it to your library part of the project.
  • You need to create a folder in the "e:" drive with the name "file". So your uploaded file will go there.

Step 7

Now check your default "web.xml" file that the same code as in the following exists there.

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>

        <servlet-name>ServletUpload</servlet-name>

        <servlet-class>ServletUpload</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>ServletUpload</servlet-name>

        <url-pattern>/ServletUpload</url-pattern>

    </servlet-mapping>

    <session-config>

        <session-timeout>

            30

        </session-timeout>

    </session-config>

</web-app>

Step 8

Now your project is ready to run.

Right-click on the project menu then choose "Run". The following output will be shown there.

Output

Step 9

Now there is an option to choose a file. So choose any file from your system as did in the following.

Choose Fig-1

Step 10

Now click on the upload button to upload your file. The following message will be shown.

File Uplaod Successfully

Note:

If you get the same message then that means you have done it successfully.

If you want to check that the file was uploaded then go to the location that we passed in the Servlet in the "MultipartRequest" part. In our servlet I passed the location as "e:/file".

Now open this folderl; you'll see your uploaded file there. As I select the file shown below.

Uploaded-File