改变世界的是这样一群人,他们寻找梦想中的乐园,当他们找不到时,他们亲手创造了它

java检测网络资源是否存在

开发中可能会遇到要从一个url取一个资源,但这个资源已经不存在了,然后就需要换源,是否需要换源首先需要检测当前网络资源是否还存在与原路径,监测代码如下:

    /**
    * 检测网络资源是否存在 
    *
    * @param strUrl
    * @return
    */
    public static boolean isNetFileAvailable(String strUrl) {
        InputStream netFileInputStream = null;
        try {
            URL url = new URL(strUrl);
            URLConnection urlConn = url.openConnection();
            netFileInputStream = urlConn.getInputStream();
            if (null != netFileInputStream) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (netFileInputStream != null){
                    netFileInputStream.close();
                }
            } catch (IOException e) {
                
            }
        }
    }

 

posted @ 2021-03-26 09:15  水狼一族  阅读(447)  评论(0编辑  收藏  举报
改变世界的是这样一群人,他们寻找梦想中的乐园,当他们找不到时,他们亲手创造了它