Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
在Java中,Sun和Apache都提供了API对字符串进行Base64编码和解码。
package com.test.day24.enanddecode; import java.io.IOException; import java.util.Arrays; import org.apache.commons.codec.binary.Base64; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * * java base64编码和解码的演示类 注:base64编码后通过url传递时,获得时"="会给替换掉, 处理方式:在编码前将"=","/","+" * 替换成别的字符,在解码之前替换回来 * * * @author tw 2010-03-01 * */ public class Base64EnAndDeCode { /** * 编码 * * @param filecontent * @return String */ public static String encode(byte[] bstr) { return new BASE64Encoder().encode(bstr); } /** * 解码 * * @param filecontent * @return string */ public static byte[] decode(String str) { byte[] bt = null; try { BASE64Decoder decoder = new BASE64Decoder(); bt = decoder.decodeBuffer(str); } catch (IOException e) { e.printStackTrace(); } return bt; } public static void apacheDecoded() { String hello = "SGVsbG8gV29ybGQ="; // // Decode a previously encoded string using decodeBase64 method and // passing the byte[] of the encoded string. // byte[] decoded = Base64.decodeBase64(hello.getBytes()); // // Print the decoded array // System.out.println(Arrays.toString(decoded)); // // Convert the decoded byte[] back to the original string and print // the result. // String decodedString = new String(decoded); System.out.println(hello + " = " + decodedString); } public static void apacheBase64Encoded() { String hello = "Hello World"; // // The encodeBase64 method take a byte[] as the paramater. The byte[] // can be from a simple string like in this example or it can be from // an image file data. // byte[] encoded = Base64.encodeBase64(hello.getBytes()); // // Print the encoded byte array // System.out.println(Arrays.toString(encoded)); // // Print the encoded string // String encodedString = new String(encoded); System.out.println(hello + " = " + encodedString); } /** * @param args */ public static void main(String[] args) { // 用sun base64对字符串进行编码 Base64EnAndDeCode te = new Base64EnAndDeCode(); String oldStr ="贞观长歌"; oldStr = te.encode(oldStr.getBytes()); System.out.println("----oldStr:" + oldStr); // 用sun base64对字符串进行解码 String str = oldStr; String str2 = new String(te.decode(str)); System.out.println("-----str2:" + str2); // 用apache codec对字符串进行Base64解码 apacheDecoded(); // 用apache codec对字符串进行Base64编码 apacheBase64Encoded(); } }
相关推荐
今天小编就为大家分享一篇关于C#使用base64对字符串进行编码和解码的测试,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
网上都是针对文件图片的Base64处理, 补充这个工具处理文本,字符串。 编码为UTF8。
简单实现js对字符串进行base64转码/解码
Base64文本字符串解码编码双向解析转换工具,网上都是针对文件的Base64处理,这个就只针对字符串文本,字符串编码为UTF8。
字符串内容编码为base64 繁殖将base64解码为字符串
代码如下: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”... <head> ...background-co
Base64字符串文本编码解码加密解密双向解析工具 网上都是针对文件图片的Base64处理,补充这个工具处理文本。 将字符串用UTF8编码为二进制,再转为Base64。 反向:将Base64解码为二进制,再用UTF8解码为字符串。
这个进行字符串加密 和解密 , 编码和 解码 包括(base64、DES、AES、SHA-1、MD5 ) ,都是亲测的,保证可用 这个进行字符串加密 和解密 , 编码和 解码 包括(base64、DES、AES、SHA-1、MD5 ) ,都是亲测的,保证...
使用base64库进行解码的步骤如下:将需要解码的Base64编码字符串转换为bytes类型。调用b64decode()函数,将Base64编码的数据进行解码,返回解码后的bytes对象。Pillow库是Python中常用的图像处理库,它的Image模块...
2. 对字符串进行BASE64解码,字符串内容是十六进制,字符不编码 3. flinksql进行调用 4. 自定义函数调用过程 4.1. 编译打包代码,生成的jar复制到flink安装目录下的lib目录。 例如:/flink/lib 4.2. flinksql脚本...
提供一种Base64编码,并输出UTF-8格式的BASE64编码方式。本程序在微信小程序开发工具中已经测试通过。 Base64代码: [javascript] view plain copy print? (function(){ var BASE64_MAPPING = [ 'A','B','C','D',...
在excel中进行Base64编解码的示例XLSM文件
java端:返回类型非字符接口调用StringUtils#base64AndCompressJson进行编码压缩 ,返回类型为字符接口调用StringUtils#base64Andcompress js 端:引入压缩包中的js文件 ,调用deBase64AndUncompress进行解压解码
1.Java生成二维码示例 2.图片保存到本地或生成Base64编码 3.Base64编码生成图片
Base64 编码解码 Java ,Java对字符串Base64 编码解码的方法!!
主要介绍了PHP安全的URL字符串base64编码和解码,在base64的基础上替换了不安全的一些字符,需要的朋友可以参考下
C++_Base64编码解码_文件_字符串..................................