package com.shi.tool; import java.io.UnsupportedEncodingException; //工具類 對字符串進行編碼 public class NewString { public static String newStr(String str){ String newstr=""; try { newstr=new String(str.getBytes("iso-8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return newstr; } }
#有時候須要對json字符串進行url編碼, 在controller中不要對齊解碼 var ids = JSON.stringify(shopIds)+""; window.location.href = Feng.ctxPath+"/shopInfo/downQRCode?shopIds=" + encodeURIComponent(ids);
解決各個瀏覽器在下載文件時出現亂碼時的兼容性問題java
String userAgent = request.getHeader("User-Agent"); if (userAgent.indexOf("MSIE") > 0 || userAgent.indexOf("Trident") > 0 || userAgent.indexOf("Edge") > 0) { fileName = fileDate + URLEncoder.encode(fileName, "UTF-8");// IE瀏覽器 (適配多個版本) }else{ fileName = fileDate + new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 其餘瀏覽器 }
出處:https://blog.csdn.net/yamadeee/article/details/83824717json