尝试使用URIUtils在java中对字符串进行URIEncode
我正在尝试使用spring框架的URIUtils在java中对字符串进行UriEncoding 据我所知,它应该很简单,但由于某种原因,字符串保持不变 我有以下字符串:尝试使用URIUtils在java中对字符串进行URIEncode,java,spring,uriencoding,Java,Spring,Uriencoding,我正在尝试使用spring框架的URIUtils在java中对字符串进行UriEncoding 据我所知,它应该很简单,但由于某种原因,字符串保持不变 我有以下字符串: http://www.foo.bar/foo/bar 我想对它进行编码 final String ENC = "UTF-8"; String uri = "http://www.foo.bar/foo/bar"; final String result = UriUtils.encodeFragment(uri, ENC);
http://www.foo.bar/foo/bar
我想对它进行编码
final String ENC = "UTF-8";
String uri = "http://www.foo.bar/foo/bar";
final String result = UriUtils.encodeFragment(uri, ENC);
结果是相同的字符串未编码
我做错了什么
如何正确地对该字符串进行URI编码,以便在get参数中使用它
我不想使用URLBuilder,因为我需要使用结果输出并为内部数据库创建哈希表
谢谢我认为使用
java.net.urlcoder
可以实现同样的效果,避免使用spring类
System.out.println(URLEncoder.encode("http://www.foo.bar/foo/bar", "UTF-8"))
将打印
http%3A%2F%2Fwww.foo.bar%2Ffoo%2Fbar
我认为使用
java.net.urlcoder
可以实现同样的效果,避免使用spring类
System.out.println(URLEncoder.encode("http://www.foo.bar/foo/bar", "UTF-8"))
将打印
http%3A%2F%2Fwww.foo.bar%2Ffoo%2Fbar
有一个RFC允许从不同参数生成URI: 因为您使用Java,所以碰巧有(免责声明:是的,它是我的) 然后你可以这样做:
// The template
final URITemplate tmpl = new URITemplate("http://my.site/foo{?params*}");
// The query parameters
// Uses Maps from Guava since the lib depends on it
final Map<String, String> params = Maps.newHashMap();
params.put("site", "http://www.foo.bar/foo/bar");
// The template data expansion
final Map<String, VariableValue> data = Maps.newHashMap();
data.put("params", new MapValue(params));
// Expand, build the URL
// The expansion takes care of all the encoding for you
final URL url = new URL(template.expand(data));
//模板
最终URITemplate tmpl=新URITemplate(“http://my.site/foo{?params*}”);
//查询参数
//使用Guava的地图,因为库依赖于它
final Map params=Maps.newHashMap();
参数put(“站点”http://www.foo.bar/foo/bar");
//模板数据扩展
最终地图数据=Maps.newHashMap();
data.put(“params”,新映射值(params));
//展开,构建URL
//扩展为您处理所有的编码
最终URL=新URL(模板.扩展(数据));
有一个RFC,允许从不同参数生成URI:
因为您使用Java,所以碰巧有(免责声明:是的,它是我的)
然后你可以这样做:
// The template
final URITemplate tmpl = new URITemplate("http://my.site/foo{?params*}");
// The query parameters
// Uses Maps from Guava since the lib depends on it
final Map<String, String> params = Maps.newHashMap();
params.put("site", "http://www.foo.bar/foo/bar");
// The template data expansion
final Map<String, VariableValue> data = Maps.newHashMap();
data.put("params", new MapValue(params));
// Expand, build the URL
// The expansion takes care of all the encoding for you
final URL url = new URL(template.expand(data));
//模板
最终URITemplate tmpl=新URITemplate(“http://my.site/foo{?params*}”);
//查询参数
//使用Guava的地图,因为库依赖于它
final Map params=Maps.newHashMap();
参数put(“站点”http://www.foo.bar/foo/bar");
//模板数据扩展
最终地图数据=Maps.newHashMap();
data.put(“params”,新映射值(params));
//展开,构建URL
//扩展为您处理所有的编码
最终URL=新URL(模板.扩展(数据));