修复文件下载流URL参数中包含中文URL编码不正确导致HTTP-400异常

This commit is contained in:
陈精华
2019-07-09 10:01:12 +08:00
committed by kl
parent e57db6925c
commit 11d6ad1ed3

View File

@@ -123,30 +123,35 @@ public class DownloadUtils {
* 对最有一个路径进行转码 * 对最有一个路径进行转码
* @param urlAddress * @param urlAddress
* http://192.168.2.111:8013/demo/Handle中文.zip * http://192.168.2.111:8013/demo/Handle中文.zip
* http://192.168.2.111:8013/download?id=1&filename=中文.zip
* @return * @return
*/ */
private String encodeUrlParam(String urlAddress){ private String encodeUrlParam(String urlAddress){
String newUrl = ""; StringBuffer sb = new StringBuffer();
try { for (int i = 0; i < urlAddress.length(); i++) {
String path = ""; char c = urlAddress.charAt(i);
String param = ""; if (c >= 0 && c <= 255) {
if (urlAddress.contains("?")) { sb.append(c);
path = urlAddress.substring(0, urlAddress.indexOf("?"));
param = urlAddress.substring(urlAddress.indexOf("?"));
} else { } else {
path = urlAddress; byte[] b;
try {
//指定需要的编码类型
b = String.valueOf(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
} }
String lastPath = path.substring(path.lastIndexOf("/") + 1); for (int j = 0; j < b.length; j++) {
String leftPath = path.substring(0, path.lastIndexOf("/") + 1); int k = b[j];
String encodeLastPath = URLEncoder.encode(lastPath, "UTF-8"); if (k < 0) {
newUrl += leftPath + encodeLastPath; k += 256;
if (urlAddress.contains("?")) {
newUrl += param;
} }
} catch (UnsupportedEncodingException e) { sb.append("%" + Integer.toHexString(k).toUpperCase());
e.printStackTrace();
} }
return newUrl; }
}
return sb.toString();
} }