은 RFC 3986 사양에 따라 일반 String
을 인코딩하는 클래스가 있습니까? "hello world"
=>"hello%20world"
하지 않음 (RFC 1738) : "hello+world"
Java 및 RFC 3986 URI 인코딩
감사
가있는 경우은 RFC 3986 사양에 따라 일반 String
을 인코딩하는 클래스가 있습니까? "hello world"
=>"hello%20world"
하지 않음 (RFC 1738) : "hello+world"
Java 및 RFC 3986 URI 인코딩
감사
가있는 경우에서 모르는입니다
. 인코딩을 제공하지만 클래스를 "+"로 변경하는 클래스가 있습니다. 그러나 String 클래스의 replaceAll 메서드를 사용하여 "+"를 원하는 것으로 변환 할 수 있습니다.
str.repaceAll은 ("+", "% 20")는 URL을의 경우
를 사용하는 URI는
URI uri = new URI("http", "//hello world", null);
String urlString = uri.toASCIIString();
System.out.println(urlString);
사실 그것은 일반적인 문자열입니다. 나는 질문을 업데이트했습니다. – Mark
음, 좋은 방법은 위에서 사용하고 앞에서 http : //를 제거하는 것입니다. – MeBigFatGuy
첫 번째 매개 변수로 null을 전달하기 만하면됩니다. – EJP
스프링 웹 응용 프로그램의 경우 abl 전자는이를 사용하는 :
UriComponentsBuilder.newInstance()
.queryParam("KEY1", "Wally's crazy empôrium=")
.queryParam("KEY2", "Horibble % sign in value")
.build().encode("UTF-8") // or .encode() defaults to UTF-8
내가 제일 좋아하는 사이트 중 하나에 십자가 검사가 "URI의 퍼센트 인코딩"같은 결과를 보여줍니다 문자열
?KEY1=Wally's%20crazy%20emp%C3%B4rium%3D&KEY2=Horibble%20%25%20sign%20in%20value
를 반환합니다. 나에게 좋아 보인다. http://rishida.net/tools/conversion/
출처 : Twitter RFC3986 호환 인코딩 기능.
이 메서드는 문자열을 받아서 RFC3986 특정 인코딩 된 문자열로 변환합니다.
/** The encoding used to represent characters as bytes. */
public static final String ENCODING = "UTF-8";
public static String percentEncode(String s) {
if (s == null) {
return "";
}
try {
return URLEncoder.encode(s, ENCODING)
// OAuth encodes some characters differently:
.replace("+", "%20").replace("*", "%2A")
.replace("%7E", "~");
// This could be done faster with more hand-crafted code.
} catch (UnsupportedEncodingException wow) {
throw new RuntimeException(wow.getMessage(), wow);
}
}
"+"에 관한 것은 아니며 쿼리 매개 변수 ("+"가 필요함)에 적합한 RFC 1738 대신 RFC 3986 사양을 완전히 따릅니다. – Mark