Base64JAVA后台编码与JS前台解码(解决中⽂乱码问题)
中⽂通过Java后台进⾏Base64编码后传到前台,通过JS进⾏Base64解码时会出现中⽂乱码的问题,被这个问题也是困扰了⼏天,使⽤jquery.base64.js只能转码⾮中⽂字符,经
过搜集各种⽅案,最终到适合⾃⼰的解决⽅案。
Java后台编码,这⾥⽤的是dec.binary.Base64:
[java]
1. private String base64Encode(String str) throws Exception {
2. if (str != null) {
3. String os = Property("os.name");
4. if (os.toLowerCase().startsWith("windows")) {
5. str = placeAll("\\r\\n", "<br/>");
6. } else if (os.toLowerCase().startsWith("linux")) {
7. str = placeAll("\\n", "<br/>");
8. }
9. return new Bytes("GBK")));
10. }
11. return null;
12. }
前台JS进⾏Base64编码与解码:
第⼀段代码主要处理中⽂字符转换:
[javascript]
1. //UnicodeAnsi.js⽂件
2. //把Unicode转成Ansi和把Ansi转换成Unicode
3. function UnicodeChr() {
4. return '00A4,00A7,00A8,00B0,00B1,00B7,00D7,00E0,00E1,00E8,00E9,00EA,00EC,00ED,00F2,00F3,00F7,00F9,00FA,00FC,0101,0113,011B,012B,014D,016B,01CE,01D0,01D2,01D4,01D6
5. }
6.
7. function AnsicodeChr() {
8. return 'A1E8,A1EC,A1A7,A1E3,A1C0,A1A4,A1C1,A8A4,A8A2,A8A8,A8A6,A8BA,A8AC,A8AA,A8B0,A8AE,A1C2,A8B4,A8B2,A8B9,A8A1,A8A5,A8A7,A8A9,A8AD,A8B1,A8A3,A8AB,A8AF,A8
9. }
10.
11. function UnicodeToAnsi(chrCode) {
12. var chrHex = String(16);
13. chrHex = "000" + UpperCase();
14. chrHex = chrHex.substr(chrHex.length - 4);
15. var i = UnicodeChr().indexOf(chrHex);
16. if (i != -1) {
17. chrHex = AnsicodeChr().substr(i, 4);
18. }
19. return parseInt(chrHex, 16);
20. }
21.
22. function AnsiToUnicode(chrCode) {
23. var chrHex = String(16);
24. chrHex = "000" + UpperCase();
25. chrHex = chrHex.substr(chrHex.length - 4);
26. var i = AnsicodeChr().indexOf(chrHex);
27. if (i != -1) {
28. chrHex = UnicodeChr().substr(i, 4);
29. }
30. return parseInt(chrHex, 16);
31. }
32.
33. // 将Unicode编码的字符串,转换成Ansi编码的字符串
34. function strUnicode2Ansi(asContents) {
35. var len1 = asContents.length;
36. var temp = "";
37. for (var i = 0; i < len1; i++) {
38. var varasc = asContents.charCodeAt(i);
39. if (varasc < 0)
40. varasc += 65536;
41. if (varasc > 127)
42. varasc = UnicodeToAnsi(varasc);
43. if (varasc > 255) {
44. var varlow = varasc & 65280;
45. varlow = varlow >> 8;
46. var varhigh = varasc & 255;
47. temp += String.fromCharCode(varlow) + String.fromCharCode(varhigh);
48. } else {
49. temp += String.fromCharCode(varasc);
50. }
51. }
52. return temp;
53. }
54.
55. // 将Ansi编码的字符串,转换成Unicode编码的字符串
56. function strAnsi2Unicode(asContents) {
57. var len1 = asContents.length;
unicode字符转中文58. var temp = "";
59. var chrcode;
60. for (var i = 0; i < len1; i++) {
61. var varasc = asContents.charCodeAt(i);
62. if (varasc > 127) {
63. chrcode = AnsiToUnicode((varasc << 8) + asContents.charCodeAt(++i));
64. } else {
65. chrcode = varasc;
66. }
67. temp += String.fromCharCode(chrcode);
68. }
69. return temp;
70. }
第⼆段代码主要进⾏Base64编码与解码:
[javascript]
1. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
2. // 将Ansi编码的字符串进⾏Base64编码
3. function encode64(input) {
4. var output = "";
5. var chr1, chr2, chr3 = "";
6. var enc1, enc2, enc3, enc4 = "";
7. var i = 0;
8.
9. do {
10. chr1 = input.charCodeAt(i++);
11. chr2 = input.charCodeAt(i++);
12. chr3 = input.charCodeAt(i++);
13.
14. enc1 = chr1 >> 2;
15. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
16. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
17. enc4 = chr3 & 63;
18.
19. if (isNaN(chr2)) {
20. enc3 = enc4 = 64;
21. } else if (isNaN(chr3)) {
22. enc4 = 64;
23. }
24.
25. output = output +
26. keyStr.charAt(enc1) +
27. keyStr.charAt(enc2) +
28. keyStr.charAt(enc3) +
29. keyStr.charAt(enc4);
30. chr1 = chr2 = chr3 = "";
31. enc1 = enc2 = enc3 = enc4 = "";
32. } while (i < input.length);
33.
34. return output;
35. }
36.
37. // 将Base64编码字符串转换成Ansi编码的字符串
38. function decode64(input) {
39. var output = "";
40. var chr1, chr2, chr3 = "";
41. var enc1, enc2, enc3, enc4 = "";
42. var i = 0;
43.
44. if (input.length % 4 != 0) {
45. return "";
46. }
47. var base64test = /[^A-Za-z0-9\+\/\=]/g;
48. if ((input)) {
49. return "";
50. }
51.
52. do {
53. enc1 = keyStr.indexOf(input.charAt(i++));
54. enc2 = keyStr.indexOf(input.charAt(i++));
55. enc3 = keyStr.indexOf(input.charAt(i++));
56. enc4 = keyStr.indexOf(input.charAt(i++));
57.
58. chr1 = (enc1 << 2) | (enc2 >> 4);
59. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
60. chr3 = ((enc3 & 3) << 6) | enc4;
61.
62. output = output + String.fromCharCode(chr1);
63.
64. if (enc3 != 64) {
65. output += String.fromCharCode(chr2);
66. }
67. if (enc4 != 64) {
68. output += String.fromCharCode(chr3);
69. }
70.
71. chr1 = chr2 = chr3 = "";
72. enc1 = enc2 = enc3 = enc4 = "";
73.
74. } while (i < input.length);
75.
76. return output;
77. }
将以上两段代码保存到两个js⽂件中,并引⼊到页⾯中就可以了。
JS进⾏Base64编码:
[javascript]
1. var strEncode = encode64(strUnicode2Ansi("JS前台编码"));
JS进⾏Base64解码:
[javascript]
1. var str=strAnsi2Unicode(decode64("xxxx"));
亲测中⽂没有乱码
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论