有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    我们也可以使用java。网url类来验证url。如果未指定协议,或找到未知协议,或规范为null,则将抛出畸形的Durlexceptio。然后我们将调用方法toURI(),如果URL无法转换为URI,它将抛出一个URISyntaxException

    class URLValidator {
        public static boolean urlValidator(String url) {
            try {
                new URL(url).toURI();
                return true;
            }
            catch (URISyntaxException exception) {
                return false;
            }
    
            catch (MalformedURLException exception) {
                return false;
            }
        }
    
        public static void main(String[] args)
        {
            String url = "https://media-cdn.tripadvisor.com/media/po/03/d8/a8/70/zdel-s.jpg"; 
            if (urlValidator(url)) 
                System.out.print("The given URL: " + url + " , contain image.");
            else
                System.out.print("The given URL: " + url + " , is not contain image.");        
        }
    }
    
  2. # 2 楼答案

    嗯,您可以使用okhttp库来维护http请求。 以下是给你的片段:

      private final OkHttpClient client = new OkHttpClient();
      public void run() throws Exception {
        Request request = new Request.Builder()
            .url("https://media-cdn.tripadvisor.com/media/po/03/d8/a8/70/zdel-s.jpg")
            .build();
    
        try (Response response = client.newCall(request).execute()) {
          if (!response.isSuccessful()) {
            //this will run if image is not available 
          }
        }
      }