Java Servlet Downloading File

2019-09-02 00:28发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.