Java URL Encoding: How to Encode Strings for URLs
Java provides the URLEncoder class in the java.net package to encode a string into a URL safe format. The URLEncoder class is used to encode a string using the format required by application/x-www-form-urlencoded MIME content type.
Here's an example of how to use the URLEncoder class to encode a string:
import java.net.URLEncoder;
public class UrlEncodeExample {
public static void main(String[] args) {
String inputString = 'Hello World!';
String encodedString = URLEncoder.encode(inputString, 'UTF-8');
System.out.println('Encoded String: ' + encodedString);
}
}
In the above example, we first import the URLEncoder class from the java.net package. We then create a string that we want to encode and pass it to the encode() method of the URLEncoder class along with the encoding format (in this case, UTF-8). The encoded string is then printed to the console.
原文地址: https://www.cveoy.top/t/topic/jZVX 著作权归作者所有。请勿转载和采集!