no

Download a File With Java and Apache Commons Io Api

To make this code works you need: 1.) http://commons.apache.org/io/download_io.cgi 2.) make sure to configure your build path and include th...

To make this code works you need: 1.) http://commons.apache.org/io/download_io.cgi 2.) make sure to configure your build path and include the apache commons io jar (I've used eclipse-java ide) 3.) the url that will be save are stored in the input/input.txt file, sample input file: [input.txt] http://google.com/file1.jpg http://google.com/file2.jpg http://google.com/file3.jpg [/input.txt]
package org.ipiel.tools.friendster.downloader;

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

import org.apache.commons.io.FileUtils;

public class PhotoDownloader {
 public static void main(String args[]) {
  new PhotoDownloader();
 }
 
 public PhotoDownloader() {
  try {
   BufferedReader lineReader = new BufferedReader(new FileReader("input/input.txt"));
   String line = "";
   int ctr = 1;
   
   while((line = lineReader.readLine()) != null) {
    URL u = new URL(line);
    String output = line.substring(line.lastIndexOf('/') + 1);
    System.out.println((ctr++) + " Downloading: " + output);
    FileUtils.copyURLToFile(u, new File(output));
   }
   
  } catch(IOException e) {
   e.printStackTrace();
  }
 }
}
One piece of advice, don't try creating your own class :-D, it's already done :-D. Thanks to the apache boys they always make my code simple :-D Well you might ask, how can I do this in C#? Read this link http://czetsuya-tech.blogspot.com/2010/03/download-image-given-url-in-c-using.html

Related

java-io 3157306963512589938

Post a Comment Default Comments

item