URL이 Java에서 유효한지 확인하는 가장 좋은 방법은 무엇입니까?
를 호출 new URL(urlString)하고 잡으려고 MalformedURLException했지만으로 시작하는 모든 것에 만족하는 것 같습니다 http://.
나는 연결을 설정하는 것에 대해 걱정하지 않고 단지 타당성을 유지합니다. 이것에 대한 방법이 있습니까? Hibernate Validator의 주석? 정규식을 사용해야합니까?
편집 :   허용되는 URL의 몇 가지 예는 http://***및 http://my favorite site!입니다.
답변
Apache Commons UrlValidator 클래스 사용 고려
UrlValidator urlValidator = new UrlValidator();
urlValidator.isValid("http://my favorite site!");
이 클래스의 작동 방식을 제어하기 위해 설정할 수있는 몇 가지 속성이 있습니다. 기본적 http으로 https, 및 ftp허용됩니다.
답변
내가 시도하고 유용하다고 생각한 방법은 다음과 같습니다.
URL u = new URL(name); // this would check for the protocol
u.toURI(); // does the extra checking required for validation of URI 
답변
Tendayi Mawushe의 답변에 대한 의견으로 이것을 게시하고 싶지만 공간이 충분하지 않은 것 같습니다.)
이것은 Apache Commons UrlValidator 소스 의 관련 부분입니다 .
/**
 * This expression derived/taken from the BNF for URI (RFC2396).
 */
private static final String URL_PATTERN =
        "/^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?/";
//         12            3  4          5       6   7        8 9
/**
 * Schema/Protocol (ie. http:, ftp:, file:, etc).
 */
private static final int PARSE_URL_SCHEME = 2;
/**
 * Includes hostname/ip and port number.
 */
private static final int PARSE_URL_AUTHORITY = 4;
private static final int PARSE_URL_PATH = 5;
private static final int PARSE_URL_QUERY = 7;
private static final int PARSE_URL_FRAGMENT = 9;
거기에서 자신의 유효성 검사기를 쉽게 만들 수 있습니다.
답변
가장 “완벽한”방법은 URL의 가용성을 확인하는 것입니다.
public boolean isURL(String url) {
  try {
     (new java.net.URL(url)).openStream().close();
     return true;
  } catch (Exception ex) { }
  return false;
}
답변
외부 라이브러리없이 내가 가장 좋아하는 접근 방식 :
try {
    URI uri = new URI(name);
    // perform checks for scheme, authority, host, etc., based on your requirements
    if ("mailto".equals(uri.getScheme()) {/*Code*/}
    if (uri.getHost() == null) {/*Code*/}
} catch (URISyntaxException e) {
}
답변
에 대한 소스 코드로 판단 URI하면
public URL(URL context, String spec, URLStreamHandler handler)
생성자는 다른 생성자보다 더 많은 유효성 검사를 수행합니다. 당신은 그것을 시도 할 수 있지만 YMMV.
답변
나는 어떤 구현도 마음에 들지 않았는데 (비용이 많이 드는 작업 인 Regex를 사용하거나 하나의 메서드 만 필요한 경우 과잉 인 라이브러리를 사용하기 때문에) 결국 java.net.URI 클래스를 일부와 함께 사용하게되었습니다. 추가 검사 및 프로토콜 제한 : http, https, file, ftp, mailto, news, urn.
그리고 예, 예외를 잡는 것은 비용이 많이 드는 작업이 될 수 있지만 정규 표현식만큼 나쁘지는 않습니다.
final static Set<String> protocols, protocolsWithHost;
static {
  protocolsWithHost = new HashSet<String>(
      Arrays.asList( new String[]{ "file", "ftp", "http", "https" } )
  );
  protocols = new HashSet<String>(
      Arrays.asList( new String[]{ "mailto", "news", "urn" } )
  );
  protocols.addAll(protocolsWithHost);
}
public static boolean isURI(String str) {
  int colon = str.indexOf(':');
  if (colon < 3)                      return false;
  String proto = str.substring(0, colon).toLowerCase();
  if (!protocols.contains(proto))     return false;
  try {
    URI uri = new URI(str);
    if (protocolsWithHost.contains(proto)) {
      if (uri.getHost() == null)      return false;
      String path = uri.getPath();
      if (path != null) {
        for (int i=path.length()-1; i >= 0; i--) {
          if ("?<>:*|\"".indexOf( path.charAt(i) ) > -1)
            return false;
        }
      }
    }
    return true;
  } catch ( Exception ex ) {}
  return false;
}
