Java的简单下载

Java的简单下载

public static void main(String[] args) throws Exception {
        String path = "http://bmob-cdn-2210.b0.upaiyun.com/2016/09/02/013a2900405a370a801dc6feebd81416.flv";
        //统一资源定位符(定位到某个服务器上的一个资源)
        URL url = new URL(path);
        //获取到连接对象
        HttpURLConnection conn  = (HttpURLConnection) url.openConnection();
        //设置连接的参数
        conn.setConnectTimeout(5000);
        //开启连接
        conn.connect();
        //获取输入输出流
        InputStream ins = conn.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File("D://sb 苍老师.avi"));
        
        int len = -1;
        byte[]bytes = new byte[512];
        
        while((len=ins.read(bytes))!=-1){
            fos.write(bytes, 0, len);
        }
        
        ins.close();
        fos.close();
        System.out.println("over");
        
    }

你可能感兴趣的:(Java的简单下载)