0

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?

Jops
  • 22,535
  • 13
  • 46
  • 63
Kevin Lee
  • 1,104
  • 3
  • 13
  • 33

3 Answers3

0

Code looks fine, so if prints true and you don't see any exceptions as well while running the program, then your problem is you are not able to locate the files copied from url.

Since no directories are specified in destination File, it must be dumping your file in the folder at which you are invoking java program. If it's an IDE (Eclipse) etc with which program is being run, refresh and check the associated project folder.

harsh
  • 7,502
  • 3
  • 31
  • 32
  • I tried that but there are no files. Network activity does suggest files are being downloaded but they are nowhere to be seen. – Kevin Lee May 05 '13 at 20:00
  • How are you running this program, by invoking `java` command or through some IDE? – harsh May 06 '13 at 06:42
0

Kevin, as you clearly didn't get any exception, here's what I suggest: please right click your Eclipse project root folder and click: Refresh. Your files will be there directly at that path.

Also, I'm removing the servlet tag from your question as the issue has totally nothing to do with servlets. It's just that you're using them inside a servlet, but this same code would work in isolation, even outside of Java EE in fact.

I added fileutils instead.

Jops
  • 22,535
  • 13
  • 46
  • 63
  • I tried that but there are no files. Network activity does suggest files are being downloaded but they are nowhere to be seen. – Kevin Lee May 05 '13 at 19:59
  • Instead of specifying file names only, try using an absolute path. – Jops May 06 '13 at 02:52
0

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.

Kevin Lee
  • 1,104
  • 3
  • 13
  • 33
  • Possibly yes, when I get the time, I'll try it myself and see. Reason could be that since calls are managed by the web container when you run your code through a web component like a servlet, it interprets paths based on its relationship to the context root, and not based on the absolute filesystem path. – Jops May 06 '13 at 11:04