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");
这样乱码就解决了。