Blog Image

Servlet and Jsp File Downloading  

In this blog we are going to see, How to download a file using servlet and jsp.
First of all create a maven web project by running below maven command.
mvn archetype:generate -DgroupId=com.srimanjavagroup -DartifactId=FileDownload -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

After creating the maven project add the bellow servlet dependency into the pom.xml of that project.


In this example we will create a jsp file and a Servlet. In the jsp file if we click on download link then it will call the FileDownload servlet, In the FileDownload servlet it search the fileName in filePath, from that file it reads the data and download.
    <h2>Hello User!!!</h2>
    <a href="download-file">click here to download the file</a>

Bellow servlet reads the content of the file and writes it into the stream to send as a response. For this purpose, we need to inform the server, so we are setting the content type as APPLICATION/OCTET-STREAM.
package com.srimanjavagroup.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileDownload extends HttpServlet {

    private static final long serialVersionUID = -3958373618055820077L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int i = 0;
        String fileName = null;
        String filePath = null;
        PrintWriter printWriter = null;
        FileInputStream fileInputStream = null;

        fileName = "file.xlsx"; /* Give any file name which you want to download */
        filePath = "D://"; /* Give the file path where your file is there */

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

        fileInputStream = new FileInputStream(filePath + fileName);

        while ((i = != -1) {

Bellow web.xml file (configuration file) provides information about the servlet.
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 ""; >

Please refer to the file attached to this blog for reference.


About author

User Image

Myself Manas Ranjan Panda and I am from Kalahandi, a District of state Odisha. I am a java Developer in one of the Software Company. I am Technically sound in Java Programming and I am very much comfortable with Web Application Development. I have good knowledge on Spring framework, Web Services, Hibernate, Restful Services, oracle, MySQL, HTML, CSS, JavaScript, Ajax, Json etc.



Be the first person to write a comment for this Blog
  • author image
      01-1-2018 05:22:19 AM

    Hi ManasPanda its really good work, i want to see more examples from you Thank you

  • author image
      27-12-2017 06:36:35 AM

    Thank's For sharing such kind of knowledge...

  • author image
      20-12-2017 06:23:38 PM

    thnx a lot for sharing useful knowledge with clear understanding..

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.