tags:

views:

97

answers:

3

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();
+2  A: 

You can change the "for" clause for a while. Just to ensure the download if the content length is not correct:

 String urlTemp = "the URL";
 File saveFile = new File("File to save path");  
 URL url = new URL(urlTemp);
 URLConnection connection = url.openConnection();
 InputStream is = connection.getInputStream();
 FileOutputStream fos = new FileOutputStream(saveFile);


 byte[] buffer = new byte[1024];
 int read = 0;
 while ((read = is.read(buffer, 0, buffer.length)) >= 0) {
     fos.write(buffer, 0, read);
 }

 fos.flush();
 fos.close();
 is.close();

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:

connection .setConnectTimeout(timeoutonnect);
connection .setReadTimeout(timeoutRead );

Hope the snippet help!

Fgblanch
Giovanni
the url is not a problem, this code will download the content of the url as a stream. The connection time out also is important if your file is not big but if the server or the connection are slow
Fgblanch
Also in your code you are downloading th file byte by byte and that's not really efficient ;)
Fgblanch
The content length is not necessarily incorrect. It's an optional header. When it is not present, then the response is usually just been transferred using chunked encoding.
BalusC
That's true, if you inspect the response to that url request in firebug you will see that the content-length is not present
Fgblanch
+3  A: 

This worked for me:

package download;

import java.io.*;
import java.net.URL;

/**
 * DownloadDemo
 * User: Michael
 * Date: Oct 11, 2010
 * Time: 10:19:34 AM
 */
public class DownloadDemo
{
    public static void main(String[] args)
    {
        StringBuilder contents = new StringBuilder(4096);
        BufferedReader br = null;

        try
        {
            String downloadSite = ((args.length > 0) ? args[0] : "http://www.google.com");
            String outputFile = ((args.length > 1) ? args[1] : "currencies.csv");
            URL url = new URL(downloadSite);
            InputStream is = url.openConnection().getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            PrintStream ps = new PrintStream(new FileOutputStream(outputFile));
            String line;
            String newline = System.getProperty("line.separator");
            while ((line = br.readLine()) != null)
            {
                contents.append(line).append(newline);
            }
            ps.println(contents.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try { if (br != null) br.close(); } catch(IOException e) { e.printStackTrace(); }
        }
    }
}

And here's part of the result (too big to fit the whole thing):

C:\JDKs\jdk1.6.0_13\bin\java -Didea.launcher.port=7533  com.intellij.rt.execution.application.AppMain download.DownloadDemo http://uif.bancaditalia.it/UICFEWebroot/QueryOneDateAllCur?lang=ita&amp;rate=0&amp;initDay=11&amp;initMonth=10&amp;initYear=2010&amp;refCur=euro&amp;R1=csv
Quotazioni in euro riferite al 11/10/2010""Paese,Valuta,Codice ISO,Codice UIC,Quotazione,Convenzione di cambio,Nota""AFGHANISTAN,Afghani,AFN,115,62.8792,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOALBANIA,Lek,ALL,047,138.163,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOALGERIA,Dinaro Algerino,DZD,106,103.035,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOANGOLA,Readjustado Kwanza,AOA,087,128.395,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOANTIGUA E BARBUDA,Dollaro Caraibi Est,XCD,137,3.76272,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOANTILLE OLANDESI,Fiorino Antille Olandesi,ANG,132,2.48061,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOARABIA SAUDITA,Riyal Saudita,SAR,075,5.22619,Foreign currency amount for 1 Euro,CAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATOARGENTINA,Peso Argentina,ARS,216,5.51578,Foreign currency amount for 1 EuroCAMBI INDICATIVI CALCOLATI GIORNALMENTE DA BI SULLA BASE DELLE RILEVAZIONI DI MERCATO

Process finished with exit code 0
duffymo
Ok but if I want save the file in cvs format can you help me??
Giovanni
It looks like it's already in CSV format, except for the newlines. I've added them in and written the results to a file in the altered code. Just clean off that leading stuff and you're good.
duffymo
+1  A: 

I would always use a library to skip this boilerplate code. This is an example with commons / io:

final String url = "http://www.etc.etc";
final String fileName = "/foo/bar/baz.txt";

InputStream in = null;
OutputStream out = null;
try{
    in = new URL(url).openStream();
    final File f = new File(fileName);
    final File par = f.getParentFile();
    if(!par.exists() && !par.mkdirs()){
        throw new IllegalStateException(
            "Couldn't create folder " + par);
    }
    out = FileUtils.openOutputStream(f);
    IOUtils.copy(in, out);
} catch(final IOException e){
    // handle exception
} finally{
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
}

Edit (duffymo): Here's the code in a form that's actually runnable:

Edit (seanizer): moved that to pastebin

seanizer
If I add in the stuff that's actually required to make your code run (e.g., imports, a class, a main method, etc.), your version ends up with 49 lines of Java code; mine has only 44. Same styles for both. I'd say mine is shorter and yours has more dependencies. I can post it if you wish.
duffymo
@duffymo really? on my machine it's my 35 ( http://pastebin.com/d7wMEfG4 ) against your 45 ( http://pastebin.com/rjecPgFC ), both including some blank lines. But that's not my point: I prefer to use descriptive high level methods where everybody immediately sees what the code does, you use highly efficient but not very communicative code. Yes: your code probably performs better, but mine is more readable and more maintainable for inexperienced developers. I'd say both approaches are valid.
seanizer
Yeah, like I said I can post it. I can edit your answer so you can see. There's other stuff in there, and I tend to have a style that doesn't minimize lines, but it's similar for both. I doubt that there's a performance difference: the network download swamps any micro-optimizations we might make. I'd guess that your library code is more performant. And your point is well taken about readability. I was just curious about your "fewer lines of code" claim. Not true, so that's not the deciding factor.
duffymo
@duffymo lines are obviously a complicated metric, as there are many ways you can write java code. What I did was to past each of our codes into eclipse and let the formatter with my standard settings do it's magic without changing the results (I took your main method and pasted into my existing class). So yes, the results are biased by my formatter settings. I'd be interested in your version, if you have the time.
seanizer
@duffymo actually I never made the claim in my answer. I said that I'm skipping the boilerplate. To me, that implies the *kind*, not the *amount* of code I am writing.
seanizer
Measured performance - it was a wash. 1210-1240 ms for both.
duffymo