How to download File in Servlet?

In the previous article, we have seen the Uploading of File using Servlet. In this article, we will discuss the Downloading of File, images from a server in Servlet.

As we know, Uploading and Downloading Features are very important in Web Application. For that purpose, we will discuss how to download a file in Servlet in the following example.

Example of File Downloading in Servlet

In this application, we will be creating two files

  • DownloadFile.java
  • index.html

We will create a project structure as shown below using Eclipse IDE

Create an index.html  file with a hyperlink Download

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  Click to download
  <a href="DownloadFile">Download</a>
</body>
</html>

Create a DownloadFile.java Servlet program

  • The First step is to locate the File path.
  • Set a response as APPLICATION/OCTET-STREAM because¬†APPLICATION/OCTET-STREAM stands for binary data(it is always good if we specify the actual file type).
  • Set Header as Content-Disposition.The content-disposition filed is added to specify the presentation style.
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
  • an attachment content-disposition, in this case, it is not displayed automatically and requires some form of action from the user to open it(in this case we say don’t open the file instead just save it ).
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/DownloadFile")
public class DownloadFile extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public DownloadFile() {
    super();
    // TODO Auto-generated constructor stub
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("in:");
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String filename = "img.jpg";
    String filepath = "E:\\java\\";
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

    FileInputStream fileInputStream = new FileInputStream(filepath + filename);

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

Now Run The program on the Server form index.html file

As soon as you click on Save It will download the file from the local host machine path which you have provided in the program and it will ask you a location to save.

Thus this is How we Download an image, file, document, etc using Servlet Programming.