首页 > 其他 > 详细

url 获取网络资源

时间:2014-05-15 17:14:31      阅读:367      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
public class Url {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        
        try {
            URL url= new URL("http://img2.haoju.cn/upfiles/201212/1355452132.jpg!mid");
            getInfo(url);
        } catch (MalformedURLException e) {
            System.out.println("您的网络出现问题了");
            e.printStackTrace();
            
        }
    }
    
    /**
     *获取url详细信息 
     * @param url
     * @throws IOException 
     */
    public static void getInfo(URL url) throws IOException{

        //方法一
        URLConnection open = url.openConnection();
        
        InputStream input = open.getInputStream();
        OutputStream output = new FileOutputStream("E:\\haoju.html");
        byte[] buffer = new byte[2048];
        int lenth = 0;
        while(-1 !=(lenth = input.read(buffer, 0, buffer.length))){
            output.write(buffer, 0, lenth);
        }
        input.close();
        output.close();
        
        //方法二
        InputStream input = url.openStream();
        OutputStream output = new FileOutputStream("E:\\1.jpg");
        byte[] buffer = new byte[2048];
        int length =0;
        while( -1!=(length =input.read(buffer, 0, buffer.length))){
            output.write(buffer, 0, length);
        }
        input.close();
        output.close();
        System.out.println("----over----");
        
        //方法三
        BufferedReader buffer = new BufferedReader(new InputStreamReader(url.openStream()));
        String line  = null;
        
        while(null != (line=buffer.readLine())){
            System.out.println(line);
        }
        buffer.close();
    }
}

bubuko.com,布布扣

参照:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951877.html

1,创建url对象

2,等到输入流

3,保存或者处理

url 获取网络资源,布布扣,bubuko.com

url 获取网络资源

原文:http://www.cnblogs.com/lihaolihao/p/3729902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!