package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import sun.misc.BASE64Encoder;
public class Test {
public static void getFile(URL u) throws IOException {
URLConnection uc = u.openConnection();
// TODO : renseigne le user / password ici
String password = "user:password";
BASE64Encoder enc = new BASE64Encoder();
String encodedPassword = enc.encode(password.getBytes());
uc.setRequestProperty( "Proxy-Authorization", "Basic " + encodedPassword );
String FileType = uc.getContentType();
System.out.println("fichier:= "+FileType);
int FileLength = uc.getContentLength();
System.out.println("taille fichier:= "+FileLength);
if (FileLength == -1) {
throw new IOException("Fichier non valide.");
}
InputStream in = uc.getInputStream();
String FileName = u.getFile();
FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
FileOutputStream WritenFile = new FileOutputStream(FileName);
byte[]buff = new byte[1024];
int l = in.read(buff);
while(l>0)
{
WritenFile.write(buff, 0, l);
l = in.read(buff);
}
WritenFile.flush();
WritenFile.close();
}
public static void main(String[] args) {
System.getProperties().put( "proxySet", "true" );
// TODO : met le nom de la machine proxy dans le 2eme paramètre
System.getProperties().put( "proxyHost", "nom de la machine servant de proxy" );
// TODO : met le port du proxy dans le 2eme paramètre
System.getProperties().put( "proxyPort", "port de ton proxy" );
try {
getFile(new URL("<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html" target="_blank">http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.ht...</a>"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}