通常我们在传递一个地址时如果有空格
String myString = "http://myhost.com/media/mp3s/9/Agenda of swine - 13. Persecution Ascension_ leave nothing standing.mp3";
URI myUri = new URI(myString);
结果会抛出异常
java.net.URISyntaxException: Illegal character in path at index X
其实很简单 只要把空格转换成别的字符就可以了
这里加设有空格的只在最后一个\ 后面
URI uri = new URI(string.replace(' ', '+'));
或者
URI uri = new URI(string.replace(" ", "%20"));
然后
int pos = string.lastIndexOf('/') + 1;
URI uri = new URI(string.substring(0, pos) + URLEncoder.encode(string.substring(pos), "UTF-8"));
796

被折叠的 条评论
为什么被折叠?



