Tuesday, January 10, 2017

Poor man's wget for Windows

Windows not only is an awfully stinking mastodon, but it even lacks the most basic tools commonly available on Linux, such as telnet and wget.

I have found a bare bones wget implementation for Java and duly simplified (I love to strip away all useless exception handling)

import java.io.InputStream;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.net.*;

public class JGet {
    public static void main(String[] args) throws Exception {
        if ((args.length != 1)) {
           System.err.println("\nUsage: java JGet [urlToGet]");
           System.exit(1);
        }
        String url = args[0];
        URL u;
        InputStream is = null;
        DataInputStream dis;
        String s;
        try {
           u = new URL(url);
           is = u.openStream();
           dis = new DataInputStream(new BufferedInputStream(is));
           while ((s = dis.readLine()) != null) {
               System.out.println(s);
           }
        } finally {
           is.close();
        }
    }
}


All the credits to Alexander.

No comments: