Dosya indirme sınıfı

Java ile http üzerindeki bir alandan dosya indirmek için aşağıdaki sınıfı kullanabilirsiniz.

    import java.io.*;
    import java.net.*;
     
    public class FileDownload {
     public static void download(String address, String localFileName) {
      OutputStream out = null;
      URLConnection conn = null;
      InputStream  in = null;
      try {
       URL url = new URL(address);
       out = new BufferedOutputStream(
        new FileOutputStream(localFileName));
       conn = url.openConnection();
       in = conn.getInputStream();
       byte[] buffer = new byte[1024];
       int numRead;
       long numWritten = 0;
       while ((numRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, numRead);
        numWritten += numRead;
       }
       System.out.println(localFileName + "\t" + numWritten);
      } catch (Exception exception) {
       exception.printStackTrace();
      } finally {
       try {
        if (in != null) {
         in.close();
        }
        if (out != null) {
         out.close();
        }
       } catch (IOException ioe) {
       }
      }
     }
     
     public static void download(String address) {
      int lastIndex = address.lastIndexOf('/');
      if (lastIndex >= 0 &&
          lastIndex < address.length()1) {
       download(address, address.substring(lastIndex + 1));
      } else {
       System.err.println("adres = " + address);
      }
     }
     
     public static void main(String[] args) {
      download("http://www.melihsakarya.com/seminer/JBoss%20Seam.pdf","c://test.pdf");
     }
    }
  1. Dec 10th, 2008 at 16:49 | #1

    Merhaba.
    jmate adinda bir yardimci minimal kutphane yaziyorum. orada su sekilde yapabiliyorsunuz bu islemi:

    new SimpleFileWriter("foo.txt")
    .copyFromURL("http://www.google.com"));

    ya da


    URL url= new URL("http://www.google.com");
    IOs.copy(url.openStream(), new FileOutputStream("google.txt"));

    ilgienenler icin.
    http://code.google.com/p/jmate/

  2. Dec 10th, 2008 at 18:17 | #2

    Gayet guzel gorunuyor :)

  3. Mevlut Becerikli
    Jun 17th, 2009 at 13:09 | #3

    Ellerine saglık işimi gördü baya baya :)

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>