How to download file using servlet

How to download file using servlet

By Yashwant Chavan, Views 13489, Date 04-Jun-2012

Some times there is requirement to download file using servlet program. File is either pdf report , xls report, jar format, word format other format

tags servlet

File Download Using Servlet Jsp

Below example demonstrate the pdf download example. Here Downloader servlet download the report.pdf file.report.pdf is should be available specified path location.

You can download any kind of file using servlet but you need change content type accordingly. To find more details on content types / MIME types MIME types

Deployment Descriptor web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Downloader</display-name>

    <servlet>
        <servlet-name>Downloader</servlet-name>
        <servlet-class>com.download.technicalkeeda.Downloader</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Downloader</servlet-name>
        <url-pattern>/Downloader</url-pattern>
    </servlet-mapping>
</web-app>

File Downloader Controller (Downloader.java)

package com.download.technicalkeeda;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

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

public class Downloader extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException {
        startDownload(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException {
        startDownload(request, response);
    }

    public void startDownload(HttpServletRequest request,
        HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException {

        File file = new File("c:/report.doc");

        ServletOutputStream stream = null;
        BufferedInputStream buf = null;
        try {
            stream = response.getOutputStream();
            // set response headers
            response.setContentType("application/msword");
            response.setDateHeader("Expires", 0);
            response.addHeader("Content-Disposition",
                "attachment; filename=report.doc");
            response.setContentLength((int) file.length());
            buf = new BufferedInputStream(new FileInputStream(file));
            int readBytes = 0;
            while ((readBytes = buf.read()) != -1)
                stream.write(readBytes);
        } finally {
            if (stream != null)
                stream.flush();
            stream.close();
            if (buf != null)
                buf.close();
        }
    }
}

Finish , Url to access the servlet Downloader. http://localhost:8080/PdfDownloder/Downloader

Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]