将文本转换为URL

时间:2011-12-21 12:10:32

标签: java url

有没有办法改变这样的请求:

"Annotation text="President Obama called Wednesday on Congress to extend a tax break
for students included in last year's economic stimulus package, arguing that the policy
provides more generous assistance."
confidence="0.2" support="20" 
sparql="SELECT DISTINCT ?x WHERE { ?x a <http://dbpedia.org/ontology/OfficeHolder>; . 
?x ?related <http://dbpedia.org/resource/Chicago>; }" 
policy="whitelist"

进入这个?

http://spotlight.dbpedia.org/rest/annotate?text=President%20Obama%20called%20Wednesday%20on%20Congress%20to%20extend%20a%20tax%20break%20for%20students%20included%20in%20last%20year%27s%20economic%20stimulus%20package,%20arguing%20that%20the%20policy%20provides%20more%20generous%20assistance.&confidence=0.2&support=20&sparql=SELECT+DISTINCT+%3Fx%0D%0AWHERE+%7B%0D%0A%3Fx+a+%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2FOfficeHolder%3E+.%0D%0A%3Fx+%3Frelated+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FChicago%3E+.%0D%0A%7D

我知道空格等于%20,但我不知道其他转换。 有没有可以进行转换的Java库?

2 个答案:

答案 0 :(得分:3)

使用

URLEncoder.encode(textAfterQuestionMark);

答案 1 :(得分:0)

encode的单参数版本标记为已弃用。为了更加确定方法将返回什么,最好指定一个编码字符集。

URLEncoder.encode( "foobar", "UTF-8" );

不同之处在于,对于单参数版本,“结果字符串可能会因平台的默认编码而异。”