问题描述
在使用URLEncoder.encode(path)构建URL请求时,把path中的空格解析为加号(+)
String path="测试 路径";
String finalPath=URLEncoder.encode(path, "UTF-8") ;
System.out.println(finalPath);
%E6%B5%8B%E8%AF%95++%E8%B7%AF%E5%BE%84
解决方案
首先了解一下特殊字符的URL编码方式
+ | %2B |
空格 | %20 |
/ | %2F |
? | %3F |
% | %25 |
# | %23 |
& | %26 |
= | %3D |
所以可以使用
finalPath=finalPath.replace("+", "%20");
手动更换+为空格