URI 中每一部分必须单独 encode。所以先把用户信息编码。
@Test
public void test() throws UnsupportedEncodingException {
String encodedUserInfo = URLEncoder.encode("hozhizhen:password", "UTF-8");
String url = "hdfs://" + encodedUserInfo + "@localhost:8020/a/b";
URI uri = URI.create(url);
String userInfo = uri.getUserInfo();
System.out.println(userInfo);
}
输出信息如下:
houzhizhen:password