ARTICLE

Download File From Server Using Servlet in Java

Posted by Sandeep Sharma Articles | NetBeans October 09, 2013
This article explains how to create an application that provides the ability to download from the server. For creating this application we use the NetBeans IDE.
Reader Level:

Introduction

This article explains how to create an application that provides the ability to download from the server. For creating this application we use the NetBeans IDE.

Downloading File from Server application

If you want to download a zip or jar file then you can provide a direct link for that and download it from that location without creating a program. If there is however any Java or jsp file etcetera that you want to download then you need to create a servlet to download that kind of file.

For creating this application we need to create the following files.

  1. HTML File
  2. Servlet File
  3. XML File

1. HTML File

A HTML file is used to provide a link for the user to download the file stored on the server.

2. Servlet File

This file provides the download facility, in this file we define the file type, file content type, filename, location, and so on information through which the server identify the file type and the details easily and this file provides a way to download the file from the server.

3. XML File

This file provides configuration information to the server about the servlet file.

Note:

In this example I used a "Downloaded.jsp" file that is stored in the "e:\" drive. This file is used for downloaded purposes. In the same way you need to create a file and save it at any location, but remember that you need to provide the correct information in the servlet file about your file (like its location, type, content type, etcetera).

In this file I write the following code.

Downloaded.jsp

<!DOCTYPE HTML>

<HTML>

<HEAD>

</HEAD>

<BODY bgcolor=grey>

<center><h1>HELLO user, this is downloaded file from server</h1>

</BODY>

</HTML>

For creating this application you need to use the following procedure.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java web" -> "Web application" as in the following.

Choose java Web

Step 3

Specify "DownloadFileApp" as the project name as in the following.

DownloadFileApp

Step 4

Choose server and Java version wizard as in the following.

Server and version wizard

Step 5

Now delete the default "index.jsp" file and create a new "index.html" file and provide the following code for it.

index.html

<!DOCTYPE html>

<html>

    <head>

        <title>File Download From Server</title>

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

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

    </head>

    <body bg color="pink">

    <center><h1>Click on below link to download file</h1>

       <a href="FileDownloadServlet">Download Here</a>

    </body>

</html>

Step 6

Now create a servlet named "FileDownloadServlet" and provide the following code for it.

FileDownloadServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class FileDownloadServlet extends HttpServlet {

 

    @Override

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("APPLICATION/OCTET-STREAM");

        PrintWriter out = response.getWriter();

        String filename = "Downloaded.jsp";

        String filepath = "e:\\";

 

        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

        FileInputStream fl = new FileInputStream(filepath + filename);

        int i;

        while ((i = fl.read()) != -1) {

            out.write(i);

        }

        fl.close();

        out.close();

    }

 

}

Step 7

Compare the code of the web.xml file with the following.

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>FileDownloadServlet</servlet-name>

        <servlet-class>FileDownloadServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>FileDownloadServlet</servlet-name>

        <url-pattern>/FileDownloadServlet</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 and choose "Run". The output is shown below.

Output

Step 9

Now click on the link given. After clicking on that link your file starts downloading as shown below.

Downloading File

Step 10

If you want to run this file just open it as shown below.

Downloaded.jsp

Note:

As I provide a string message in this file so its shown in the browser.

COMMENT USING
Employers - Post Free Jobs