首页

关于java使用URLConnection获取网络资源大小及内容使用

标签:URLConnection,URL,net网络包,HttpURLConnection,java,BufferedReader,InputStream,InputStreamReader,IO流,io,网络通信     发布时间:2015-07-02   

前言

jdk提供java.net包下面的URL类和URLConnection类实现获取网络资源信息,java.net.URL作为同一资源定位符,是指向互联网“资源”的指针,而java.net.URLConnection类代表程序和URL之间建立通信的链接通道。通过链接实例可对读取写入URL引用的网络资源。

示例代码

1.实现获取网站上图片资源大小及相关信息的实现代码如下:

import java.io.IOException;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.net.URLConnection;@b@@b@public class GetResourceInfoByURL {@b@@b@    /**@b@     * @param args@b@     */@b@    public static void main(String[] args) {@b@        // TODO Auto-generated method stub@b@        try {@b@            URL url=new URL("http://www.xwood.net/xwood-editor/ueditor/jsp/upload/image/20150522/1432224808458083168.gif");@b@            URLConnection conn=url.openConnection();@b@            conn.connect();@b@            //显示资源大小,单位字节@b@            System.out.println(conn.getContentLength());@b@            //显示资源内容类型@b@            System.out.println(conn.getContentType());@b@            //显示资源内容编码@b@            System.out.println(conn.getContentEncoding());@b@        } catch (MalformedURLException e) {@b@            // TODO Auto-generated catch block@b@            e.printStackTrace();@b@        }catch (IOException e) {@b@            // TODO Auto-generated catch block@b@            e.printStackTrace();@b@        }@b@@b@    }@b@@b@}

运行结果:

16525@b@image/gif@b@null

2.获取新浪网订阅信息,并对内容进行过滤解析,示例代码如下:

import java.io.BufferedReader;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.net.HttpURLConnection;@b@import java.net.URL;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@public class GetResourceContentByHttpURLConnection {@b@@b@    public static void main(String[] args) throws Exception {@b@@b@        String httpurl = "http://news.sina.com.cn/c/2011-09-27/141123224906.shtml";@b@        String currentLine = "";@b@        String totalString = "";@b@        InputStream urlStream;@b@        String content = "";@b@        @b@        StringBuffer contentBuf=new StringBuffer();@b@        try@b@        {@b@            URL url = new URL(httpurl);@b@            HttpURLConnection connection = (HttpURLConnection) url.openConnection();@b@            connection.connect();@b@            urlStream = connection.getInputStream();@b@            BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream, "GBK"));@b@            while ((currentLine = reader.readLine()) != null)@b@            {@b@                if(currentLine.indexOf("分享 begin")!=-1)@b@                    break;@b@                if(currentLine.indexOf("<p>")!=-1){@b@                    if(currentLine.indexOf("<p>")>currentLine.indexOf("正文块 begin"))@b@                            contentBuf.append(currentLine);@b@                }@b@            }@b@            totalString=contentBuf.toString();@b@            totalString=totalString.substring(0, totalString.lastIndexOf("</p>")+4);@b@            System.out.println(totalString);@b@            content = totalString;@b@            Pattern p = Pattern.compile("<p>");@b@            Matcher m = p.matcher(content);@b@            while (m.find())@b@            {@b@                String temp = m.group();@b@            }@b@        } catch (Exception e)@b@        {@b@            e.printStackTrace();@b@        }@b@    }@b@@b@}