Spring File Down-loader

File download is very common to any type of web application. Let’s see how this done in spring framework and java. This is pretty easy and you can add/remove code segments as you want. The given code segments are written as I thought and comments will give you an idea about each code.

Let’s go then, Please follow below simple steps to create your own file down-loader;

Step 1:

Create the spring controller.

  @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
  public void getUserProfilePage(@RequestParam("fileName") String fileName,
  HttpServletRequest request, HttpServletResponse response) {
    FileDownloadManager downloadManager = new FileDownloadManager();
    try {
      downloadManager.downloadFile(fileName, response);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

Step 2:

Create a java Class “DownloadManager” and create below method.

public void downloadFile(String fileName, HttpServletResponse response) throws IOException {
    // reads input file from an absolute path
    try {
      //get fixed file path from the property file of the project
      String filePath = CommonUtil.getValueFromFile("application", "file.upload.location") + fileName;
      File downloadFile = new File(filePath);
      FileInputStream inStream = new FileInputStream(downloadFile);

      // gets MIME type of the file
      String mimeType = new MimetypesFileTypeMap().getContentType(downloadFile);
      if (mimeType == null) {
        // set to binary type if MIME mapping not found
        mimeType = "application/octet-stream";
      } else if (StringUtils.equals(FilenameUtils.getExtension(filePath), "pdf")
          || StringUtils.equals(FilenameUtils.getExtension(filePath), "PDF")) {
        mimeType = "application/pdf";
      }
      System.out.println("MIME type: " + mimeType);

      // modifies response
      response.setContentType(mimeType);
      response.setHeader("Content-Disposition", "inline; filename=" + downloadFile.getName());
      response.setContentLength((int) downloadFile.length());

      // if you want to forces download you can use below
      // String headerKey = "Content-Disposition";
      // String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
      // response.setHeader(headerKey, headerValue);

      // obtains response's output stream
      OutputStream outStream = response.getOutputStream();

      byte[] buffer = new byte[4096];
      int bytesRead = -1;

      while ((bytesRead = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
      }

      inStream.close();
      outStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

For mime type; you can use different java libraries like MimeMagic, Tika or use URLConnection. But I have used simple MimetypesFileTypeMap which is available form java 1.7 as I know.

Now you can enjoy the file down-loader by accessing the spring controller via browser. Please contact me for further information.

Leave a comment