码迷,mamicode.com
首页 > 其他好文 > 详细

BASE64解密加密

时间:2015-07-28 19:05:50      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

package com.mai.base64;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

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

public class Base64Test {

    private String imageURL = "c:/test.png";
    @Test
    public void testBase64Encoder(){
        BASE64Encoder encoder = new BASE64Encoder();
        
        try {
            StringBuilder pictureBuffer = new StringBuilder();
            InputStream input = new FileInputStream(new File(imageURL));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            for(int len = input.read(temp); len != -1;len = input.read(temp)){
                out.write(temp, 0, len);
                pictureBuffer.append(encoder.encode(out.toByteArray()));
                //out(pictureBuffer.toString());
                out.reset();
            }
            
            out(pictureBuffer.toString());
            out("Encoding the picture Success");
            
            
        
        BASE64Decoder decoder = new BASE64Decoder();
        FileOutputStream write = new FileOutputStream(new File("c:/test2.png"));
        byte[] decoderBytes = decoder.decodeBuffer(pictureBuffer.toString());
        write.write(decoderBytes);
        out("Decoding the picture Success");
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    
    public void out(Object o){
        System.out.println(o.toString());
    }
}

 

技术分享


BASE64解密加密

标签:

原文地址:http://my.oschina.net/ruibo/blog/484761

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!