URLUtil

Introduction

URL (Uniform Resource Locator) is a Chinese name for a uniform resource locator, sometimes also commonly referred to as a web address. It represents resources on the Internet, such as web pages or FTP addresses. In Java, URLs can also be used to represent resources (Resources) addresses in the Classpath.

Methods

Obtaining URL Objects

  • URLUtil.url Creates an object from a string-based URL address
  • URLUtil.getURL Primarily obtains the URL of resources in the ClassPath to facilitate reading configuration files and other information in the ClassPath.

Others

  • URLUtil.normalize Normalizes URL links. Simply completes addresses without http:// headers.
String url = "http://www.hutool.cn//aaa/bbb";
// Result: http://www.hutool.cn/aaa/bbb
String normalize = URLUtil.normalize(url);

url = "http://www.hutool.cn//aaa/\\bbb?a=1&b=2";
// Result: http://www.hutool.cn/aaa/bbb?a=1&b=2
normalize = URLUtil.normalize(url);
  • URLUtil.encode Encapsulates URLEncoder.encode, converts the content that needs to be converted (content outside of ASCII code form) into hexadecimal representation, and prefixes it with %.
String body = "366466 - 副本.jpg";
// Result: 366466%20-%20%E5%89%AF%E6%9C%AC.jpg
String encode = URLUtil.encode(body);
  • URLUtil.decode Encapsulates URLDecoder.decode, decodes hexadecimal content that starts with %.
  • URLUtil.getPath Obtains the path component URI -> http://www.aaa.bbb/search?scope=ccc&q=ddd PATH -> /search
  • URLUtil.toURI Converts a URL or URL string to a URI.