Search code examples
javafiledownloadfileutils

Java Servlet Downloading File


So I have two files, the servlet:

package com.servlets;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

import org.apache.commons.io.FileUtils;

import com.java.DataDownloader;

/**
 * Servlet implementation class downloaderServ
 */
public class DownloaderServ extends HttpServlet {
    private static final long serialVersionUID = 1L;
    DataDownloader dl;
/**
 * @see HttpServlet#HttpServlet()
 */
public DownloaderServ() {
    super();
    dl = new DataDownloader();
    // TODO Auto-generated constructor stub
}

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            dl.download();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
    }

}

The application which does the processing:

package com.java;
import java.io.*;
import java.net.*;
import org.apache.commons.io.*;

public class DataDownloader {

private static boolean get(String address, String fileName) {
    try {
            URL url = new URL(address);
            File f = new File(fileName);
            FileUtils.copyURLToFile(url, f);
    }
    catch(MalformedURLException e) {
            System.out.println(e);
            return false;
    }
    catch(IOException e) {
            System.out.println(e);
            return false;
    }

    return true;
}

public boolean download() {

    String[][] urls = new String[3][2];

    urls[0][0] = "http://data.london.gov.uk/datafiles/crime-community-safety/mps-recordedcrime-borough.csv";
    urls[0][1] = "crimes.csv";
    urls[1][0] = "http://data.london.gov.uk/datafiles/housing/average-house-prices-borough.xls";
    urls[1][1] = "prices.xls";
    urls[2][0] = "http://data.london.gov.uk/datastorefiles/datafiles/demographics/gla_2012rnd_SHLAA_based_borough_projections.xls";
    urls[2][1] = "population.xls";

    for (int i = 0; i < 3; i++) {
            if (get(urls[i][0], urls[i][1]) == false) {
                    System.out.println(false);
                    return false;
            }
    }
    return true;
}

}

I can run it with no problems but there does not seem to be any files downloaded. I have also printed out the return values (true or false) and it does print true. Is downloading a file not as simple as this?


Solution

  • I changed the it so an absolute path is taken e.g.

    File f = new File("C:\\data\\" + fileName);
    

    This works. Does having it in a servlet change it so an absolute path is needed and render relative paths unusable? I tested the downloading part outside of a servlet and it works with relative paths or it just downloads into project folder if nothing is specified.