1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| * //
* import java.net.*;
* import java.io.*;
*
* public class UrlUtils {
*
* public UrlUtils(String HOST) {
*
* try {
* URL racine = new URL(HOST);
* getFile(racine);
* } catch (MalformedURLException e) {
* System.err.println(HOST + " : URL non comprise.");
* } catch (IOException e) {
* System.err.println(e);
* }
*
* }
*
* public void getFile(URL u) throws IOException {
* URLConnection uc = u.openConnection();
* String FileType = uc.getContentType();
* int FileLenght = uc.getContentLength();
* if (FileLenght == -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();
*
* }
*
* } |
Partager