Download File From Server Using Servlet in Java


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.


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.






<BODY bgcolor=grey>

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



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.


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.


<!DOCTYPE html>



        <title>File Download From Server</title>

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

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


    <body bg color="pink">

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

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



Step 6

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


import javax.servlet.*;

import javax.servlet.http.*;


public class FileDownloadServlet extends HttpServlet {



    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {


        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 = != -1) {








Step 7

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


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

<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">















Step 8

Now your project is ready to run.

Right-click on the project menu and choose "Run". The output is shown below.


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.



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

Up Next
    Ebook Download
    View all
    View all