Uploading File to the Sever Using JSP in Java

Introduction

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

File-Uploading to the Server using JSP

For a file-uploading app we need the following files:

  • index.jsp file
  • process.jsp File
  • cos.jar file

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

2. The process.jsp file processes the file and uploads it to the server

3. The "COS.JAR" jar file provides the access to use the MultipartRequest class. This class is used in the JSP file to upload a file.

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 Application

Step 3

Provide your project the name "UploadFileJSPApp" as in the following.

UploadFileJSPApp

Step 4

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

Server and Version Wizard

Step 5

Replace the default "index.jsp" file code with the following code.

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <style type="text/css">

            body {background-color: khaki;}           

        </style>

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

        <title>File Upload App in JSP</title>

    </head>

    <body>

        <form action="process.jsp" method="post" enctype="multipart/form-data">

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

            <input type="submit" value="Upload">

        </form>

    </body>

</html> 

Step 6

Create another JSP file named  "process" with the following code.

process.jsp

<%@page import="com.oreilly.servlet.MultipartRequest" %>

<%

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

    out.println("Successfully Uploaded..");

    %> 

Note

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

Step 7

Now your project is ready to run.

Right-click on the "index.jsp" file then choose run. The following output will be shown.

Output

Step 8

Now there is an option for choosing file. So choose any file from your system as in the following as I did.

Choosing File

Step 9

Now click on upload button to upload your file. The following message is shown.

Uploading Message

Note:

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

If you want to check that the file was upload then go to the location that we passed in the "process.jsp" file in the "MultipartRequest" part. In this I passed the location as "e:/file".

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

OnlineTest.java File

Up Next
    Ebook Download
    View all
    Learn
    View all