Grails框架+Intellij IDEA工具,写了一个对字符串进行转码,包括纯js转成Base64格式

1.Controller

1 def show(){
2         System.err.println("进行转码....");
3         String des = params.description;
4         Base64Encoder encoder = new Base64Encoder();
5         def s  = des.getBytes("utf-8").encodeBase64().toString();
6         System.err.println("转码成功....,转换后"+s);
7         [description:s];
8     }

2.js

  1  <script type="text/javascript">
  2             function conversion(){
  3                 var des = document.getElementById("des").value;
  4                 var b  = new Base64();
  5                 var test =b.encode(des);
  6                 document.getElementById("show").innerText ="转码后的结果为:"+ test;
  7             }
  8             function Base64() {
  9 
 10                 // private property
 11                 _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 12 
 13                 // public method for encoding
 14                 this.encode = function (input) {
 15                     var output = "";
 16                     var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
 17                     var i = 0;
 18                     input = _utf8_encode(input);
 19                     while (i < input.length) {
 20                         chr1 = input.charCodeAt(i++);
 21                         chr2 = input.charCodeAt(i++);
 22                         chr3 = input.charCodeAt(i++);
 23                         enc1 = chr1 >> 2;
 24                         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
 25                         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
 26                         enc4 = chr3 & 63;
 27                         if (isNaN(chr2)) {
 28                             enc3 = enc4 = 64;
 29                         } else if (isNaN(chr3)) {
 30                             enc4 = 64;
 31                         }
 32                         output = output +
 33                                 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
 34                                 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
 35                     }
 36                     return output;
 37                 }
 38 
 39                 // public method for decoding
 40                 this.decode = function (input) {
 41                     var output = "";
 42                     var chr1, chr2, chr3;
 43                     var enc1, enc2, enc3, enc4;
 44                     var i = 0;
 45                     input = input.replace(/[^A-Za-z0-9+/=]/g, "");
 46                     while (i < input.length) {
 47                         enc1 = _keyStr.indexOf(input.charAt(i++));
 48                         enc2 = _keyStr.indexOf(input.charAt(i++));
 49                         enc3 = _keyStr.indexOf(input.charAt(i++));
 50                         enc4 = _keyStr.indexOf(input.charAt(i++));
 51                         chr1 = (enc1 << 2) | (enc2 >> 4);
 52                         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
 53                         chr3 = ((enc3 & 3) << 6) | enc4;
 54                         output = output + String.fromCharCode(chr1);
 55                         if (enc3 != 64) {
 56                             output = output + String.fromCharCode(chr2);
 57                         }
 58                         if (enc4 != 64) {
 59                             output = output + String.fromCharCode(chr3);
 60                         }
 61                     }
 62                     output = _utf8_decode(output);
 63                     return output;
 64                 }
 65 
 66                 // private method for UTF-8 encoding
 67                 _utf8_encode = function (string) {
 68                     string = string.replace(/
/g,"
");
 69                     var utftext = "";
 70                     for (var n = 0; n < string.length; n++) {
 71                         var c = string.charCodeAt(n);
 72                         if (c < 128) {
 73                             utftext += String.fromCharCode(c);
 74                         } else if((c > 127) && (c < 2048)) {
 75                             utftext += String.fromCharCode((c >> 6) | 192);
 76                             utftext += String.fromCharCode((c & 63) | 128);
 77                         } else {
 78                             utftext += String.fromCharCode((c >> 12) | 224);
 79                             utftext += String.fromCharCode(((c >> 6) & 63) | 128);
 80                             utftext += String.fromCharCode((c & 63) | 128);
 81                         }
 82 
 83                     }
 84                     return utftext;
 85                 }
 86 
 87                 // private method for UTF-8 decoding
 88                 _utf8_decode = function (utftext) {
 89                     var string = "";
 90                     var i = 0;
 91                     var c = c1 = c2 = 0;
 92                     while ( i < utftext.length ) {
 93                         c = utftext.charCodeAt(i);
 94                         if (c < 128) {
 95                             string += String.fromCharCode(c);
 96                             i++;
 97                         } else if((c > 191) && (c < 224)) {
 98                             c2 = utftext.charCodeAt(i+1);
 99                             string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
100                             i += 2;
101                         } else {
102                             c2 = utftext.charCodeAt(i+1);
103                             c3 = utftext.charCodeAt(i+2);
104                             string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
105                             i += 3;
106                         }
107                     }
108                     return string;
109                 }
110             }
111 
112         </script>

3.gsp:url的意思是提交到EncodeController这个控制器,调用show这个方法

1  <div>
2         <g:form url="[controller:'encode',action:'show']" name="ti">
3               <g:textArea name="description" id="des"></g:textArea>
4               <input value="encode" type="button" onclick="conversion()"/>
5               <input type="submit" value="提交"/>
6         </g:form>
7         <span id="show"> </span>
8     </div>

  注:纯js转成base64是Copy过来的

  

如果有使用请标明来源:http://www.cnblogs.com/duwenlei/
原文地址:https://www.cnblogs.com/duwenlei/p/3654542.html