- 论坛徽章:
- 0
|
[Java]代码- /**
- *
- * @Title: netRourceAccessable
- * @author dengwenbing
- * @Description: 判断一个网络资源是否有效
- *
- * @param source
- * @return
- */
- public static boolean netRourceAccessable(String source) {
- try {
- URL url = new URL(source); // 创建URL对象。
- URLConnection uc = url.openConnection(); // 创建一个连接对象。
- InputStream in = uc.getInputStream(); // 获取连接对象输入字节流。如果地址无效则会抛出异常。
- System.out.println(source);
- System.out.println(uc.getURL().toString());
- if (!source.equalsIgnoreCase(uc.getURL().toString())) {
- return false; // 用于请求地址是否重定向。
- }
- in.close();
- return true;
- } catch (Exception e) {
- System.out.println("截图路径不存在:source={},exception={}");
- e.printStackTrace();
- return false;
- }
- }
复制代码 |
|