我有字符串Ruby On Rails
,应该转换为Ruby%20On%20Rails
,但URLEncoder
说Ruby+On+Rails
。这是我的尝试:
String encoded = URLEncoder.encode("Ruby On Rails");
System.out.println(encoded);
是否因为encode()
方法已被弃用而发生?
答案 0 :(得分:2)
URLEncoder不进行URI编码,而application/x-www-form-urlencoded
则将空格转换为+
符号。如果您希望将它们转换为%20
,则需要使用URI编码的内容,例如Spring's UriUtils。