Cela a fonctionné pour moi:
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(); }
}
}
}
Et voici une partie du résultat (trop gros pour tenir la chose):
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&rate=0&initDay=11&initMonth=10&initYear=2010&refCur=euro&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
le fichier est très faible. Le problème que je pense est la chaîne d'adresse http://uif.bancaditalia.it/UICFEWebroot/QueryOneDateAllCur?lang=ita&rate=0&initDay=11&initMonth=10&initYear=2010&refCur=euro&R1=csv n'est pas un fichier mais un php ou une requête similaire – Giovanni
le url n'est pas un problème, ce code va télécharger le contenu de l'URL en tant que flux. Le délai de connexion est également important si votre fichier n'est pas grand mais si le serveur ou la connexion sont lents – Fgblanch
Aussi dans votre code vous téléchargez le fichier octet par octet et ce n'est pas vraiment efficace;) – Fgblanch