Created
July 10, 2015 08:55
-
-
Save wangyingang/38c4dccf395d8a2a59ae to your computer and use it in GitHub Desktop.
URL escape sample (java.net.URLEncoder and Guava UrlEscaper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UrlEscapeSample { | |
public static void main(String[] args) { | |
String filename = "中国 地图/? a.docx"; | |
String urlEncoding = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()); | |
prettyPrint("java.net.URLEncoder", urlEncoding); | |
prettyPrint("UrlEscapers.urlFormParameterEscaper", UrlEscapers.urlFormParameterEscaper().escape(filename)); | |
prettyPrint("java.net.URLEncoder then replace to %20", urlEncoding.replaceAll("\\+", "%20")); | |
prettyPrint("UrlEscapers.urlPathSegmentEscaper", UrlEscapers.urlPathSegmentEscaper().escape(filename)); | |
prettyPrint("UrlEscapers.urlFragmentEscaper", UrlEscapers.urlFragmentEscaper().escape(filename)); | |
} | |
static void prettyPrint(String label, String value) { | |
System.out.printf("%-40s%-10s%s\n", label, "-->", value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment