package com.example.demo; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; /** * Created by Administrator on 2019/6/19. */ public class URLEncode { public static void main(String[] args) { System.out.println(ChangeISOToUTF("http://luod.james.cn/gis/rest//%E8%A1%8C%E6%94%BF%E5%8C%BA%E5%88%92/test")); System.out.println(ChangeUTFToISO("http://luod.james.cn/gis/rest//测试/test")); } public static String ChangeUTFToISO(String serverurlUTF) { StringBuffer resultStr = new StringBuffer(); for (int i = 0; i < serverurlUTF.length(); i++) { char c = serverurlUTF.charAt(i); if (c >= 0 && c <= 255) { resultStr.append(c); } else { byte[] b; try { b = String.valueOf(c).getBytes("utf-8"); } catch (Exception ex) { System.out.println(ex); b = new byte[0]; } for (int j = 0; j < b.length; j++) { int k = b[j]; if (k < 0) k += 256; resultStr.append("%" + Integer.toHexString(k).toUpperCase()); } } } String serverurlISO = resultStr.toString(); return serverurlISO; } /** * 将服务地址中url ASCII字符集 改成 中文 * @param serverurlISO * @return */ public static String ChangeISOToUTF(String serverurlISO) { String serverurlUTF; try { serverurlUTF = URLDecoder.decode(serverurlISO,"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); serverurlUTF = ""; } return serverurlUTF; } }