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.

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
</dependency>

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.
index.jsp
<html>
<body>
    <h2>Hello User!!!</h2>
    <a href="download-file">click here to download the file</a>
</body>
</html>

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.
FileDownload.java
package com.srimanjavagroup.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

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.setContentType("text/html");
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        fileInputStream = new FileInputStream(filePath + fileName);

        while ((i = fileInputStream.read()) != -1) {
            printWriter.write(i);
        }
        fileInputStream.close();
        printWriter.close();
    }
}

Bellow web.xml file (configuration file) provides information about the servlet.
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd"; >
<web-app>
    <servlet>
        <servlet-name>downloadServlet</servlet-name>
        <servlet-class>com.srimanjavagroup.servlet.FileDownload</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>downloadServlet</servlet-name>
        <url-pattern>/download-file</url-pattern>
    </servlet-mapping>
</web-app>

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

Attachments

About author

User Image
ManasPanda

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.

3

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      surya1
      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
    by:
      Pratik4551
      27-12-2017 06:36:35 AM

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

  • author image
    by:
      ali032
      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.