Commit 11d6ad1e authored by 陈精华's avatar 陈精华 Committed by kl

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

parent e57db692
...@@ -123,30 +123,35 @@ public class DownloadUtils { ...@@ -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) {
String newUrl = ""; private String encodeUrlParam(String urlAddress){
try { StringBuffer sb = new StringBuffer();
String path = ""; for (int i = 0; i < urlAddress.length(); i++) {
String param = ""; char c = urlAddress.charAt(i);
if (urlAddress.contains("?")) { if (c >= 0 && c <= 255) {
path = urlAddress.substring(0, urlAddress.indexOf("?")); sb.append(c);
param = urlAddress.substring(urlAddress.indexOf("?")); } else {
}else { byte[] b;
path = urlAddress; try {
} //指定需要的编码类型
String lastPath = path.substring(path.lastIndexOf("/") + 1); b = String.valueOf(c).getBytes("utf-8");
String leftPath = path.substring(0, path.lastIndexOf("/") + 1); } catch (Exception ex) {
String encodeLastPath = URLEncoder.encode(lastPath, "UTF-8"); System.out.println(ex);
newUrl += leftPath + encodeLastPath; b = new byte[0];
if (urlAddress.contains("?")) { }
newUrl += param; for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) {
k += 256;
}
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
} }
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} }
return newUrl; return sb.toString();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment