Posted By

jerryvig on 10/21/11


Tagged

java unix download external process wget


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dineshviswanath


Java Wget Example


 / Published in: Java
 

An example that shows how to download files with wget from a Java program calling an external process. The key is read the output of process from the ErrorStream and not the InputStream.

  1. public class DLTest {
  2. public static void main( String[] args ) {
  3. try {
  4. BufferedReader adUrlReader = new BufferedReader( new FileReader( "./AdUrls.csv" ) );
  5. ArrayList<String> adUrlList = new ArrayList<String>();
  6. String line;
  7.  
  8. while ( (line = adUrlReader.readLine()) != null ) {
  9. adUrlList.add( line.trim() );
  10. }
  11. adUrlReader.close();
  12.  
  13. int fileCounter = 0;
  14. for ( String adUrl : adUrlList ) {
  15. try {
  16. String cmdString = "/usr/bin/wget -v --output-document=/home/diggler/Desktop/groovy_testing/data_scrapes/Admonitor/DL/file" + Integer.to\
  17. String(fileCounter) + " --tries=3 " + adUrl;
  18. System.out.println( cmdString );
  19. Runtime rt = Runtime.getRuntime();
  20. Process p = rt.exec( cmdString );
  21. p.waitFor();
  22. BufferedReader r = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  23. String s;
  24. while ((s = r.readLine())!=null) {
  25. System.out.println( s );
  26. }
  27. r.close();
  28. } catch ( IOException ioe ) { ioe.printStackTrace(); }
  29. catch ( InterruptedException ie ) { ie.printStackTrace(); }
  30. fileCounter++;
  31. }
  32.  
  33. } catch ( IOException e ) { e.printStackTrace(); }
  34. }
  35. }

Report this snippet  

You need to login to post a comment.