當前位置:首頁 » 編程語言 » javaurl判斷

javaurl判斷

發布時間: 2023-05-16 23:35:05

java 判斷url是否有效 hutool

try{
URL url=new URL("http://1111.com");
url.openConnection();
}catch(Exception e){
System.out.println("鏈接無效");
}
System.out.println("鏈接有效"寬敗備);
//枯御如果不能打開鏈接,自慎毀然無效,就會執行Catch程序,否則便是有效,自然程序繼續往下走了

Ⅱ 用Java判斷一個URL是否有效的兩種方法

import java.io.InputStream;
import java.net.URL;

public class Test {
public static void main(String[] args) {
URL url;
try {
url = new URL("http://www..com");
InputStream in = url.openStream();
System.out.println("連接可用");
} catch (Exception e1) {
System.out.println("連接打不開!");
url = null;
}
}
}

2
import java.net.HttpURLConnection;
import java.net.URL;

/**
* 文件名稱為:URLAvailability.java
* 文件功能簡述: 描述一個URL地址是否有效
* @author Jason
* @time 2010-9-14
*
*/
public class URLAvailability {
private static URL url;
private static HttpURLConnection con;
private static int state = -1;

/**
* 功能:檢測當前URL是否可連接或是否有效,
* 描述:最多連接網路 5 次, 如果 5 次都不成功,視為該地址不可用
* @param urlStr 指定URL網路地址
* @return URL
*/
public synchronized URL isConnect(String urlStr) {
int counts = 0;
if (urlStr == null || urlStr.length() <= 0) {
return null;
}
while (counts < 5) {
try {
url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
state = con.getResponseCode();
System.out.println(counts +"= "+state);
if (state == 200) {
System.out.println("URL可用!");
}
break;
}catch (Exception ex) {
counts++;
System.out.println("URL不可用,連接第 "+counts+" 次");
urlStr = null;
continue;
}
}
return url;
}
public static void main(String[] args) {
URLAvailability u=new URLAvailability();
u.isConnect("http://www..com");
}
}

Ⅲ Java判斷一個鏈接是否有效

一、春鬧
/**
*判斷鏈接是否有效
*輸入鏈接
*返回段迅true或者false
*/
publicstaticbooleanisValid(StringstrLink){
URLurl;
try{
url=newURL(strLink);
HttpURLConnectionconnt=(HttpURLConnection)url.openConnection();
connt.setRequestMethod("HEAD");
StringstrMessage=connt.getResponseMessage();
if(strMessage.compareTo("NotFound")==0){
returnfalse;
}
connt.disconnect();
}catch(Exceptione){
returnfalse;
}
returntrue;
}
二、
packagetest;
importJava'target='_blank'>java.net.*;
publicclass握森此riqi{
publicstaticvoidmain(String[]args){
try{
URLurl=newURL("http://www.9iyyzm.com");
URLConnectionconn=url.openConnection();
Stringstr=conn.getHeaderField(0);
if(str.indexOf("OK")>0)
{
System.out.println("www.2cto.com正常!");
}else{
System.out.println("不能游覽");
}
}catch(Exceptionex){
}

Ⅳ java 判斷url 是不是文件

public class FileUtils {
/**
* 檢測網路資源是否存在
*
* @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) {
}
}
}
}

Ⅳ 用Java判斷一個URL是否有效的兩種方法

/**
*驗證URL合法性
橡畢派*
*@paramurl
*@returntrue:合法false:不合法
*/
publicstaticbooleancheckUrl(Stringurl){
returnurl.matches("^((https|http|ftp|rtsp|mms)?://)"
+"+(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?"
+"(([0-9]{1,3}\.){3}[0-9]{1,3}"+"|"+"([0-9a-z_!~*'()-]+\.)*"
梁賀+"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."數告+"[a-z]{2,6})"+"(:[0-9]{1,10})?"
+"((/?)|"+"(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$");
}

Ⅵ 如何用java判斷一個url是不是PDF類型的文件

我不知道該如何判斷  但我有一個很笨的方法
如果是一個下載文件 後台會有下面的操作(J2EE,其他平台也一樣,答改重點是括弧裡面的參數)
res.setContentType("application/x-download;charset=gbk");
res.setHeader("Content-Disposition" ,"attachment;filename=test.pdf");
得到鏈接之後需要訪問每個鏈接 使用socket的輸入流獲差悄取每個鏈接的頭信息  根據頭信息判斷是否為pdf文件
但這樣有一個bug  有些網站給文件取名字不一定就是以真正文件的後綴名結尾 
其次 一些壓縮文件裡面可能也有pdf文清慶判件  這樣的pdf文件是搜索不出來的

Ⅶ java判斷url 是否是視頻鏈接

判斷文件的後綴名啊,視頻一般是MP4,flv,avi,rm,rmvb,.....看此手是不是這些後綴名就行啦 在URL中判斷"."後面的字悔簡符內容,即可知是否是視頻碧扒褲連接

Ⅷ Java怎麼判斷URL是域名格式還是IP加埠格式

判斷方法:
out;
TODO
Auto-generated
catch
block
e,那就是說當做的域名,戚空枝如果解析出來的虧源IP和域名字元串相等.printStackTrace();
}
catch
(MalformedURLException
e)
{
/簡單的辦法是判斷host格式.cn"
target="_blank">http;/://www,其實是ip.out.com,復雜高敏的但穩妥的辦法是當做域名處理.getByName(host);)://www;
else
System;
TODO
Auto-generated
catch
block
e;
InetAddress
address
=
null;/.cn");
if(host.getHostAddress()))
System;domain".println(".printStackTrace();).println(".equalsIgnoreCase(address;
address
=
InetAddress,域名解析;ip".sina.getHost();
}
catch
(UnknownHostException
e)
{
/.com.sina。
try
{
URL
url=new
URL("
String
host=url

Ⅸ Java中判斷URL地址的圖片是否有效

下面給你分享一個小工具類,可以幫到你

  • /**

  • *判斷網路圖片是否存在

  • *posturl圖片地址鏈接

  • */

  • (Stringposturl)throwsIOException{

  • URLurl=newURL(posturl);

  • HttpURLConnectionurlcon=(HttpURLConnection)url.openConnection();

  • urlcon.setRequestMethod("POST");

  • urlcon.setRequestProperty("Content-type",

  • "application/x-www-form-urlencoded");

  • if(urlcon.getResponseCode()==HttpURLConnection.HTTP_OK){

  • System.out.println(HttpURLConnection.HTTP_OK+posturl

  • +":postedok!");

  • return"200";

  • }else{

  • System.out.println(urlcon.getResponseCode()+posturl

  • +":Badpost...");

  • return"404";

  • }

  • }

上傳圖片的時候會生成一個圖片地址鏈接,把這個鏈接調用這個工具類進行判斷,如果返回200,則圖片上傳成功,返回其他則圖片地址有誤,或者上傳失敗。

熱點內容
安卓如何打開shell窗口 發布:2025-07-16 09:28:09 瀏覽:311
華為榮耀備忘錄文件夾 發布:2025-07-16 09:23:23 瀏覽:972
基於特徵匹配演算法 發布:2025-07-16 09:18:23 瀏覽:46
夢香神奇寶貝伺服器的ip 發布:2025-07-16 09:14:07 瀏覽:212
電子密碼手套箱是什麼 發布:2025-07-16 09:13:27 瀏覽:799
手機連接資料庫 發布:2025-07-16 09:13:23 瀏覽:132
廣東伺服器存儲虛擬主機 發布:2025-07-16 09:13:17 瀏覽:326
絕地逃亡電腦怎麼設置最低配置 發布:2025-07-16 09:10:50 瀏覽:425
聯想z5的配置如何 發布:2025-07-16 09:08:21 瀏覽:879
電腦加密軟體哪個好 發布:2025-07-16 09:05:47 瀏覽:320