CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-core

Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.

Pending
Overview
Eval results
Files

url-operations.mddocs/

URL Operations

URL creation, parsing, encoding, and manipulation utilities through the URLUtil class.

Capabilities

URL Creation and Conversion

Create URL objects from various sources and convert between URL types.

/**
 * Create URL from string
 * @param url URL string
 * @return URL object
 */
public static URL url(String url);

/**
 * Create URL from URI
 * @param uri URI object
 * @return URL object
 */
public static URL url(URI uri);

/**
 * Create URL with custom stream handler
 * @param url URL string
 * @param handler custom URL stream handler
 * @return URL object
 */
public static URL url(String url, URLStreamHandler handler);

/**
 * Convert URL string to HTTP URL (add http:// if missing)
 * @param urlStr URL string
 * @return URL object with HTTP protocol
 */
public static URL toUrlForHttp(String urlStr);

/**
 * Convert URL to URI
 * @param url URL object
 * @return URI object
 */
public static URI toURI(URL url);

/**
 * Convert URL to URI with encoding control
 * @param url URL object
 * @param isEncode whether to encode the URL
 * @return URI object
 */
public static URI toURI(URL url, boolean isEncode);

Usage Examples:

import cn.hutool.core.util.URLUtil;
import java.net.URL;
import java.net.URI;

// Create URLs
URL httpUrl = URLUtil.url("https://example.com/path");
URL fileUrl = URLUtil.url("file:///home/user/file.txt");

// Auto-add HTTP protocol
URL autoHttp = URLUtil.toUrlForHttp("example.com/page"); // becomes http://example.com/page

// URL to URI conversion
URI uri = URLUtil.toURI(httpUrl);

URL Encoding and Decoding

Handle URL encoding and decoding with proper character handling.

/**
 * Decode URL string
 * @param url encoded URL string
 * @return decoded URL string
 */
public static String decode(String url);

/**
 * Encode blank spaces in URL
 * @param urlStr URL string with spaces
 * @return URL string with encoded spaces
 */
public static String encodeBlank(CharSequence urlStr);

/**
 * Get decoded path from URL
 * @param url URL object
 * @return decoded path component
 */
public static String getDecodedPath(URL url);

/**
 * Get path from URI string
 * @param uriStr URI string
 * @return path component
 */
public static String getPath(String uriStr);

Usage Examples:

import cn.hutool.core.util.URLUtil;
import java.net.URL;

// URL encoding/decoding
String encoded = URLUtil.encodeBlank("https://example.com/path with spaces");
String decoded = URLUtil.decode("https://example.com/path%20with%20spaces");

// Path extraction
URL url = URLUtil.url("https://example.com/user/profile?id=123");
String path = URLUtil.getDecodedPath(url); // "/user/profile"
String pathFromUri = URLUtil.getPath("https://example.com/api/users"); // "/api/users"

URL Normalization and Validation

Normalize URL formats and validate URL types.

/**
 * Normalize URL string
 * @param url URL string to normalize
 * @return normalized URL string
 */
public static String normalize(String url);

/**
 * Normalize URL with path encoding control
 * @param url URL string to normalize
 * @param isEncodePath whether to encode path components
 * @return normalized URL string
 */
public static String normalize(String url, boolean isEncodePath);

/**
 * Normalize URL with full control
 * @param url URL string to normalize
 * @param isEncodePath whether to encode path components
 * @param replaceSlash whether to replace backslashes with forward slashes
 * @return normalized URL string
 */
public static String normalize(String url, boolean isEncodePath, boolean replaceSlash);

/**
 * Check if URL is a file URL
 * @param url URL to check
 * @return true if URL uses file protocol
 */
public static boolean isFileURL(URL url);

/**
 * Check if URL is a JAR URL
 * @param url URL to check
 * @return true if URL points to JAR content
 */
public static boolean isJarURL(URL url);

/**
 * Check if URL is a JAR file URL
 * @param url URL to check
 * @return true if URL points to JAR file
 */
public static boolean isJarFileURL(URL url);

Usage Examples:

import cn.hutool.core.util.URLUtil;
import java.net.URL;

// URL normalization
String messy = "http://example.com//path/../other/./file.html";
String clean = URLUtil.normalize(messy); // "http://example.com/other/file.html"

String withBackslashes = "http://example.com\\path\\file.html";
String normalized = URLUtil.normalize(withBackslashes, false, true); // "http://example.com/path/file.html"

// URL type checking
URL fileUrl = URLUtil.url("file:///home/user/file.txt");
URL jarUrl = URLUtil.url("jar:file:/app.jar!/config.properties");

boolean isFile = URLUtil.isFileURL(fileUrl); // true
boolean isJar = URLUtil.isJarURL(jarUrl); // true

URL Content Access

Access content from URLs and handle different URL types.

/**
 * Get input stream from URL
 * @param url URL to read from
 * @return InputStream for URL content
 */
public static InputStream getStream(URL url);

/**
 * Get buffered reader from URL
 * @param url URL to read from
 * @param charset character encoding
 * @return BufferedReader for URL content
 */
public static BufferedReader getReader(URL url, Charset charset);

/**
 * Get JAR file from JAR URL
 * @param url JAR URL
 * @return JarFile object
 */
public static JarFile getJarFile(URL url);

/**
 * Get connection from URL
 * @param url URL to connect to
 * @return URLConnection object
 */
public static URLConnection getConnection(URL url);

Usage Examples:

import cn.hutool.core.util.URLUtil;
import java.io.InputStream;
import java.io.BufferedReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.jar.JarFile;

// Read from URL
URL configUrl = URLUtil.url("https://example.com/config.json");
try (InputStream stream = URLUtil.getStream(configUrl)) {
    // Process stream content
}

// Read text from URL
URL textUrl = URLUtil.url("https://example.com/data.txt");
try (BufferedReader reader = URLUtil.getReader(textUrl, StandardCharsets.UTF_8)) {
    String line = reader.readLine();
}

// Access JAR file
URL jarUrl = URLUtil.url("jar:file:/app.jar!/");
JarFile jarFile = URLUtil.getJarFile(jarUrl);

URL Host and Component Extraction

Extract components from URLs and manipulate URL parts.

/**
 * Get host URI from URL
 * @param url URL to extract host from
 * @return URI with host information only
 */
public static URI getHost(URL url);

/**
 * Build query string from parameters
 * @param params parameter map
 * @param charset character encoding
 * @return query string
 */
public static String buildQuery(Map<String, Object> params, Charset charset);

/**
 * Parse query string to map
 * @param query query string to parse
 * @param charset character encoding
 * @return map of query parameters
 */
public static Map<String, String> parseQuery(String query, Charset charset);

Usage Examples:

import cn.hutool.core.util.URLUtil;
import java.net.URL;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.HashMap;

// Extract host
URL fullUrl = URLUtil.url("https://api.example.com:8443/v1/users?page=1");
URI hostUri = URLUtil.getHost(fullUrl); // https://api.example.com:8443

// Query string operations
Map<String, Object> params = new HashMap<>();
params.put("page", 1);
params.put("size", 10);
params.put("search", "john doe");

String queryString = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
// Result: "page=1&size=10&search=john+doe"

Map<String, String> parsed = URLUtil.parseQuery(queryString, StandardCharsets.UTF_8);
// Result: {"page": "1", "size": "10", "search": "john doe"}

Common Types

// URL and networking types
import java.net.URL;
import java.net.URI;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.util.jar.JarFile;
import java.nio.charset.Charset;

// URL protocol constants
public static final String CLASSPATH_URL_PREFIX = "classpath:";
public static final String FILE_URL_PREFIX = "file:";
public static final String JAR_URL_PREFIX = "jar:";
public static final String WAR_URL_PREFIX = "war:";
public static final String URL_PROTOCOL_FILE = "file";
public static final String URL_PROTOCOL_JAR = "jar";
public static final String URL_PROTOCOL_WAR = "war";
public static final String URL_PROTOCOL_ZIP = "zip";

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-core

docs

collection-operations.md

compression-operations.md

date-time-operations.md

file-io-operations.md

index.md

object-operations.md

random-operations.md

string-operations.md

type-conversion.md

url-operations.md

xml-operations.md

tile.json