Java Servlet Downloading File -
so have 2 files, 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 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"; (int = 0; < 3; i++) { if (get(urls[i][0], urls[i][1]) == false) { system.out.println(false); return false; } } return true; }
}
i can run no problems there not seem files downloaded. have printed out return values (true or false) , print true. downloading file not simple this?
code looks fine, if prints true
, don't see exceptions while running program, problem not able locate files copied url.
since no directories specified in destination file
, must dumping file in folder @ invoking java
program. if it's ide (eclipse) etc program being run, refresh , check associated project folder.
Comments
Post a Comment