2016年11月1日火曜日

HttpFileDownload 练习

HttpFileDownload 练习

https://paiza.io/projects/6lTQWbqAzvT-IEAHaRl4AA



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

public class Main {
    public static void main(String[] args) throws Exception {

    // https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be
    // https://images-na.ssl-images-amazon.com/images/I/81M-I12yh7L._SL1500_.jpg
    // http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip

    httpd h = new httpd("https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be");
    h.std();
    }
}

class httpd{
    private String durl;
   
    public httpd(String durl){
        this.durl = durl;
    }

    public void std(){
        System.out.println("sdt()-start");
        try{
        FileOutputStream fis = new FileOutputStream("demo.db");
        InputStream is = dis();
        byte[] buff = new byte[10240];
        int len = 0;
       
        System.out.println("koko");
        while( (len = is.read(buff)) != -1){
            fis.write(buff,0,len);
            fis.flush();
            System.out.println(len);
        }
       
        fis.close();
        }catch(Exception e){
           
        }finally{
            File f = new File("demo.db");
            System.out.println(f.getName());
            System.out.println(f.getAbsolutePath());
        }
    }

    public InputStream dis(){
        InputStream inputStream = null;
        try{
            URL url = new URL(durl);
            System.out.println("url = " + durl);
            HttpURLConnection httpurl = (HttpURLConnection)url.openConnection();
            httpurl.setConnectTimeout(3000);
            httpurl.setDoInput(true);
            httpurl.setReadTimeout(3000);
            httpurl.setRequestMethod("GET");
            if(httpurl.getResponseCode() == 200){
                System.out.println("paus" + httpurl.getResponseCode());
                inputStream = httpurl.getInputStream();
            }
        }catch(Exception e){
        }
        return inputStream;
    }
}

0 件のコメント: