java中BASE64编码实例

核心提示:java Base64编码非常容易,jdk本身就提供了base64编码的类,下面是base64编码和解码的例子 例子中使用参数-e表示加密,-d表示解密. 代码 import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import sun.m

java Base64编码非常容易,jdk本身就提供了base64编码的类,下面是base64编码和解码的例子

例子中使用参数-e表示加密,-d表示解密.

代码
import  java.io.ByteArrayInputStream;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;

import  sun.misc.BASE64Decoder;
import  sun.misc.BASE64Encoder;


public   class  Base64 {

    
/**
     * 
@param  args
     
*/
    
public   static   void  main(String[] args)  throws  Exception {
        
//  TODO Auto-generated method stub        
         if (args.length == 2 ){
            
if ( " -e " .equals(args[ 1 ])){
                //System.out.println(
" encoding./r/n/r/n " );
                BASE64Encoder base64
= new  BASE64Encoder();
                base64.encode(args[
0 ].getBytes( " utf-8 " ), System.out);
            }
else   if ( " -d " .equals(args[ 1 ])){
                //System.out.println(
" decoding./r/n/r/n " );
                BASE64Decoder base64
= new  BASE64Decoder();
                base64.decodeBuffer(
new  ByteArrayInputStream(args[ 0 ].getBytes( " utf-8 " )), System.out);
            }
        }
else   if (args.length == 3 ){
            
if ( " -e " .equals(args[ 2 ])){
                //System.out.println(
" encoding./r/n/r/n " );
                BASE64Encoder base64
= new  BASE64Encoder();
                base64.encode(
new  FileInputStream(args[ 0 ]),  new  FileOutputStream(args[ 1 ], false ));
            }
else   if ( " -d " .equals(args[ 2 ])){
                //System.out.println(
" decoding./r/n/r/n " );
                BASE64Decoder base64
= new  BASE64Decoder();
                base64.decodeBuffer(
new  FileInputStream(args[ 0 ]),  new  FileOutputStream(args[ 1 ], false ));
            }
        }
else  
            System.out.println(
" used c://>java Base64 inputString -e or c://>java Base64 inputFile outputFile -d " );
        System.out.flush();
        System.out.println(
" /r/n/r/n " );
    }

}

 轻松完成了字符串的编码解码以及文件的编码解码

了解Base64,请  http://zh.wikipedia.org/wiki/Base64
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值