URL

来源:互联网 发布:阿里云ace下线原因 编辑:程序博客网 时间:2024/05/29 02:13

网址

package com.lingzhuo.url;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;public class BaiduUrl {    public static void main(String[] args) {        try {            URL url=new URL("http://www.baidu.com/");            InputStream is=url.openStream();            BufferedReader br=new BufferedReader(new InputStreamReader(is));            String line=br.readLine();            if(line!=null){                System.out.println(line);                line=br.readLine();            }        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

这里写图片描述

图片

package com.lingzhuo.url;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;public class ImgUrl {    public static void main(String[] args) {        OutputStream os = null;        File file = new File("d:\\img.jpg");        if (!file.exists()) {            try {                file.createNewFile();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        try {            os = new FileOutputStream(file);            URL url = new URL(                    "http://h.hiphotos.baidu.com/image/pic/item/f9dcd100baa1cd119d029327bb12c8fcc3ce2d05.jpg");            InputStream is = url.openStream();            byte[] array = new byte[1024];            int i = is.read(array);            while (i != -1) {                os.write(array, 0, i);                i = is.read(array);            }            os.flush();            is.close();            os.close();        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

这里写图片描述

0 0
原创粉丝点击