Hi I need some help to download a file from my java application.
The URL is "http://my.site.com/UICFEWebroot/QueryOneDateAllCur?lang=ita&rate=0&initDay=11&initMonth=10&initYear=2010&refCur=euro&R1=csv"
I try using this code but the result is an empty file
URL urlAgg = new URL(address);
int lf = urlAgg.openConnection().getContentLength();
FileOutputStream fos = new FileOutputStream("agg" + File.separator + "cambio" + gg + mm + aaaa + ".csv");
InputStream in = urlAgg.openStream();
for (int i = 0; i < lf; i++)
{
byte[] b = new byte[1];
in.read(b);
fos.write(b);
}
fos.close();
in.close();
This worked for me:
And here's part of the result (too big to fit the whole thing):
I would always use a library to skip this boilerplate code. This is an example with commons / io:
Edit (duffymo): Here's the code in a form that's actually runnable:
Edit (seanizer): moved that to pastebin
You can change the "for" clause for a while. Just to ensure the download if the content length is not correct:
Also try /catch section will be needed. If the file to down load is big you may need to set a bigger time out on the connection object:
Hope the snippet help!