base64字符串和文件之间的转换

package com.hy.springmvc.utils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.springframework.util.Base64Utils;

public class Base64AndImg {
    
    
    public static String imgToBase64(String path) throws IOException {
        String ret = null;
        FileInputStream fis = new FileInputStream(path);
        byte[] b = new byte [fis.available()];
        fis.read(b);
        fis.close();
        ret = Base64Utils.encodeToString(b);
        return ret;
    }
    
    public static OutputStream base64ToImg(String base64Str) throws IOException {
        OutputStream os = new FileOutputStream("E:\\11.png");
        byte[] b = Base64Utils.decodeFromString(base64Str);
        os.write(b);
        os.flush();
        os.close();
        return os;
    }
}

 

这里使用的spring的base64Utils 

常用的有sun.misc的和apche的

posted on 2017-03-10 17:26  _故乡的原风景  阅读(1559)  评论(0编辑  收藏  举报