zoukankan      html  css  js  c++  java
  • url 获取网络资源

    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();
        }
    }

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

    1,创建url对象

    2,等到输入流

    3,保存或者处理

  • 相关阅读:
    cvBox2D和RotatedRect中返回的角度angle详解
    opencv源码阅读之——iOS的两条接口UIImageToMat()和MatToUIImage()
    3、设置jsp上的类容自动更新
    2、搭建一个简单的Web项目
    1、IDEA的常用快捷键
    2、jQuery的Ajax简单示例
    1、jQuery的使用入门
    13、Ajax的使用
    JSP和后台交互时的乱码问题
    12、Filter(拦截器)
  • 原文地址:https://www.cnblogs.com/lihaolihao/p/3729902.html
Copyright © 2011-2022 走看看