从URL中读取数据

最后更新 19/9/28

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class SourceViewer {
    public static void main(String...args){
        InputStream in = null;
        try{
            URL u = new URL("http://www.baidu.com");
            in = u.openStream();
            BufferedInputStream bin = new BufferedInputStream(in);
            Reader r = new InputStreamReader(in);
            int c;
            while((c = r.read())!=-1)
                System.out.print(((char)c));

        }catch(MalformedURLException m){
            System.err.println("it is not a parseable URL");

        }catch(IOException ex){
            System.out.println(ex);
        }finally {
            if(in!=null)
                try{
                    in.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
        }
    }
}

该程序下载百度网页的html
使用URL的方法 openStream() 它会返回一个InputStream,从这个流读取数据
openConnection() 返回可以配置的URLConnection,再由它得到一个InputStream
getContent()向URL请求内容,如String Image 返回一个InputStream

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

//getContent() 与 getContent(Type)
public class ContentGetter {
    static URL u;
    static InputStream in;
    public static void main(String...args){
        //getContent()会返回一个string 或 reader 或 inputStream 或其他 很难预测
        try{
           u = new URL("http://www.baidu.com");
            Object o = u.getContent();
            System.out.println(o.getClass().getName());
        }catch (MalformedURLException e){
            System.err.println("it isn't a URL");
        }catch(IOException e){
            e.printStackTrace();
        }



        //用getContent(Type)可以选择希望的返回值类型
        //UTF-8
        Class<?> []types = new Class[3];
        types[0] = InputStream.class;
        types[1] = Reader.class;
        types[2] = String.class;
        try{
            u = new URL("http://www.baidu.com");
            Object o1 = u.getContent(types);
            if(o1 instanceof String){
                System.out.println("1");
                System.out.println(o1);
            }
            if(o1 instanceof Reader){
                System.out.println("2");
                int c;
                Reader r = (Reader)o1;
                while((c=r.read())!=-1){
                    System.out.print((char)c);
                }
                r.close();
            }
            if(o1 instanceof InputStream){
                System.out.println("3");
                int c;
                InputStream in = (InputStream) o1;
                Reader r = new InputStreamReader(in,"UTF-8");
                while((c=r.read())!=-1){
                    System.out.print((char)c);
                }
                in.close();
            }
        }catch(MalformedURLException e){
            System.err.println("it isn't a URL");
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

getContent() :URL指示的为某文本(html,ascii),返回一个inputStream 如果指示图像 返回java.awt.ImageProducer.由于返回值不确定,可以采用带有参数的getContent(Class<?>[] c),表示希望的返回值类型

(未完待续)

发布了23 篇原创文章 · 获赞 4 · 访问量 844

猜你喜欢

转载自blog.csdn.net/qq_43656529/article/details/101384574