当前位置: 代码迷 >> JavaScript >> Java跟JS转码
  详细解决方案

Java跟JS转码

热度:383   发布时间:2012-12-18 12:43:41.0
Java和JS转码

怕忘记。

转为ASCII形式:

String str="你是";
		try {
			System.out.println(URLEncoder.encode(str, "UTF-8"));  //结果为%E4%BD%A0%E6%98%AF
			System.out.println(URLDecoder.decode("%E4%BD%A0%E6%98%AF","UTF-8"));  //结果为你是     这个里面的字符为URLEncoder.encode(str, "UTF-8")产生
			System.out.println(URLDecoder.decode("%E4%BD%A0%E6%98%AF","UTF-8"));  //结果为你是     这个里面的字符为javascript中encodeURI('你是')产生
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

?HTML中的Js代码:

<script type="text/javascript">
 		function test_encodeURI(){
			alert(encodeURI('你是'));	// 到jsp页面中解码的话用URLDecoder.decode即可,具体参看类URLEncodeTest.java
			document.write(encodeURI('你是')+"<br>");
			alert(decodeURI('%E4%BD%A0%E6%98%AF'));	// encodeURIComponent
			document.write(encodeURI('www.cup.edu.cn')+"<br>");
			document.write(encodeURIComponent('http://www.cup.edu.cn')+"<br>");    //http%3A%2F%2Fwww.cup.edu.cn
			document.write(decodeURI('www.cup.edu.cn')+"<br>");
			document.write(decodeURIComponent('http%3A%2F%2Fwww.cup.edu.cn')+"<br>");   //http://www.cup.edu.cn
 		}
</script>

?

  相关解决方案