Best HTTP Solution:
public static boolean exists(String URLName){ try { HttpURLConnection.setFollowRedirects(false); // note : you may also need // HttpURLConnection.setInstanceFollowRedirects(false) HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { e.printStackTrace(); return false; } }
If you are looking for some other url try this code
public static boolean exists(String URLName){ boolean result = false; try { url = new URL("ftp://ftp1.freebsd.org/pub/FreeBSD/"); //url = new URL("ftp://ftp1.freebsd.org/pub/FreeBSD123/");//this will fail input = url.openStream(); System.out.println("SUCCESS"); result = true; } catch (Exception ex) { Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex); } return result; }
Source : http://www.rgagnon.com/javadetails/java-0059.html
Jigar Joshi Nov 14 2018-10-14 14:17
source share