%-hex编码URI的正确方法是什么?

Ada*_*ort 0 java http

我使用Java servlet创建了一个基本服务器.我想要做的是通过允许URI中的非字母数字字符实现更自由的URI语法,只要它们是%-hex编码即两个单词.txt可以编码为2%words.txt.

我不知道该如何解决这个问题.我尝试以uri.replace('','%')的形式使用java的.replace,但这给了我一个消息格式异常.

tho*_*ens 5

看看http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html

String encoded = URLEncoder.encode("This string has spaces", "UTF-8");
Run Code Online (Sandbox Code Playgroud)