/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package httpfileget1;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.lang.reflect.Proxy;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.SocketAddress;
import java.net.URL;

/**
 *
 * @author emiddio
 */
public class Main {

    static byte[] ba = new byte[1024];
    private static boolean POST = false;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws MalformedURLException, IOException {
        // TODO code application logic here
        URL u = new URL("http://localhost:8080/wgfaSftCapture3.wmv");
        HttpURLConnection huc2 = (HttpURLConnection) u.openConnection();
        HttpURLConnection huc = huc2;
        if (false) {
            InetSocketAddress isa = new InetSocketAddress("amd4800", 8888);
            SocketAddress sa = isa;
            java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, sa);
            huc = (HttpURLConnection) u.openConnection(p);//proxy
        }
        System.out.println("DoInput default:" + huc.getDoInput());
        System.out.println("DoOutput default:" + huc.getDoOutput());
        huc.setRequestMethod("GET");//will be POST if doing output
        if (POST) {
            huc.setDoOutput(true);
        }
        huc.connect();
        if (POST) {
            OutputStream os = huc.getOutputStream();
            OutputStreamWriter out = new OutputStreamWriter(os);
            out.close();
        }
        InputStream is = huc.getInputStream();
        System.out.println("ContentEncoding:" + huc.getContentEncoding());
        System.out.println("ContentType:" + huc.getContentType());
        System.out.println("ContentLength:" + huc.getContentLength());
        System.out.println("ConnectTimeout:" + huc.getConnectTimeout());
        System.out.println("ReadTimeout:" + huc.getReadTimeout());


        Object o = huc.getContent();
        System.out.println("Content.toString:" + o.toString());
        if (false) {
            InputStreamReader isr = new InputStreamReader(is);
            Reader reader = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
        }
        BufferedInputStream bis = new BufferedInputStream(is);
        File f = new File("wgfaSftCapture3.wmv");
        FileOutputStream fos = new FileOutputStream(f);

        int c;
        int rc = 0;
        int cnt = 0;
        if (false) {
            while ((c = bis.read()) != -1) {
                cnt++;
                fos.write(c);
                //System.out.print((char) c);
            }
        }
        System.out.println(new java.util.Date().toString());
        try {
            while ((c = bis.read(ba)) != -1) {
                rc++;
                cnt += c;
                fos.write(ba);
                Thread.sleep(100);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        System.out.println(new java.util.Date().toString());

        bis.close();
        fos.close();
        System.out.format("rc:%d, cnt:%d\n", rc, cnt);
    }
}