2016年11月2日水曜日

ByteArrayOutputStream 练习

ByteArrayOutputStream 练习


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
    // http://download.oracle.com/technetwork/java/javase/6/docs/zh/api.zip
    // http://cdefgab.web.fc2.com/song.json

    httpd h = new httpd("http://cdefgab.web.fc2.com/song.json");
    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");
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        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();
            bao.write(buff,0,len);
            System.out.println(len);
        }
     
        fis.close();
        System.out.println(bao.toString());
        }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 件のコメント: