崇之他和她

导航

day021EXERCISE

复习练习exercise

public static void main(String[] args) {
    Date st = new Date(System.currentTimeMillis());
    //倒计时
    int a = 16;
    while (true){
        System.out.println(a--+"系统当前时间:"+new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").format(st));
        try {
            if (a<=0){
                break;
            }
            Thread.sleep(1000);
            st = new Date(System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

通过url下载图片其中new URL(参数地址) 传入的字符串参数需要可靠,否则报错

public void downFile(String urlstr, String name) {
    try {
        URL url = new URL(urlstr);//这里的url需要可靠
        InputStream is = url.openStream();
        System.out.println(is);
        FileUtils.copyURLToFile(url, new File(name));
        System.out.println("下载成功" + name);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    //*********下面是报错演示
    //1.url丢失/ java.lang.IllegalArgumentException: protocol = https host = null
new WebDownLoad(
    "https:/dss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=147409438,3456107658&fm=55&app=54&f=JPEG?w=1140&h=640", "下载.jpg").start();
    //2.url 不存在java.net.MalformedURLException: no protocol: http.c
new WebDownLoad("http.c", "下载图片.jpg").start();

posted on 2021-01-26 18:36  崇之他和她  阅读(74)  评论(0编辑  收藏  举报