1. js 如何給中文轉碼
需要准備的材料分別有:電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器,版新建html文件,例如:index.html。
2. 怎麼對url連接進行URL 編碼
在js中可以使用escape(), encodeURL(), encodeURIComponent(),三種方法都有一些不會被編碼的符號:
escape():@ * / +
encodeURL():! @ # $& * ( ) = : / ; ? + '
encodeURIComponent():! * ( ) '
在java端可以使用URLDecoder.decode(「中文」, "UTF-8");來進行解碼
但是由於使用request.getParameter()來獲取參數時已經對編碼進行了一次解碼,所以一般情況下只要在js中使用
encodeURIComponent("中文");
在java端直接使用request.getParameter()來獲取即可返回中文。
如果你想在java端使用URLDecoder.decode(「中文」, "UTF-8");來解碼也可以在js中進行二次編碼,即:
encodeURIComponent(encodeURIComponent("中文"));
如果不進行二次編碼的話,在java端通過decode方法取的會是亂碼。
3. java中有沒有類似decodeURI()的方法
Javascript 中的 decodeURI/decodeURIComponent,Java中的java.net.URLDecoder類的decode方法提供了相近的功能,但又有一些微妙的不同:
varURLDecoder=Java.type("java.net.URLDecoder");
varURLEncoder=Java.type("java.net.URLEncoder");
varurl="
中文";
vars1=encodeURI(url);
vars2=encodeURIComponent(url);
vars3=URLEncoder.encode(url,"UTF-8");
print(s1);
print(s2);
print(s3);
print("-------------")
print(decodeURI(s1));
print(decodeURI(s2));
print(decodeURI(s3));
print("-------------")
print(decodeURIComponent(s1));
print(decodeURIComponent(s2));
print(decodeURIComponent(s3));
print("-------------")
print(URLDecoder.decode(s1,"UTF-8"));
print(URLDecoder.decode(s2,"UTF-8"));
print(URLDecoder.decode(s3,"UTF-8"));
使用JDK8的jjs.exe運行上面這段代碼,結果如下:
D:Temp>j:sharejdk8injjs.exeurl.js
http%3A%2F%2Fwww.example.com%2Fstring%20with%20%2B%20and%20%3F%20and%20%26%20and%20%E4%B8%AD%E6%96%87
http%3A%2F%2Fwww.example.com%2Fstring+with+%2B+and+%3F+and+%26+and+%E4%B8%AD%E6%96%87
-------------
中文
http%3A%2F%2Fwww.example.com%2Fstringwith%2Band%3Fand%26and中文
http%3A%2F%2Fwww.example.com%2Fstring+with+%2B+and+%3F+and+%26+and+中文
-------------
中文
中文
中文
-------------
中文
中文
中文
4. urlencoder.encode,"utf-8" 編碼 js什麼解碼
1、漢字出現在URL路徑部分的時候不需要編碼解碼;
2、使用encodeURI進行2次編碼;
3、在openModelDialog()打開的模式窗體里沒辦法用request.getParameter正確獲取參數;
客戶端和伺服器在傳遞數據時可以用過濾器filter解決字元編碼問題,但filter只能解決post方式提交的數據。對於get方式,可以使用兩次encodeURI(encodeURI(「中文」))並在伺服器中使用URLDecoder.decode(「中文」,
"UTF-8");
今天用Ajax校驗數據時也遇到這個問題,盡管頁面、類和web容器都統一了字元編碼,提交的數據依然是亂碼,所以就採用了2次encodeURI()編碼方式,亂碼問題就解決了。
在頁面中:
/exportExcel.topinfo?ls="+encodeURI(encodeURI(_tmplsgx))+"&zt="+encodeURI(encodeURI(_tmpzt))
在action中
String ls=request.getParameter("ls");
ls = new String(ls.getBytes("iso-8859-1"),"utf-8");
ls = java.net.URLDecoder.decode(ls,"UTF-8");
這樣亂碼就解決了。