import android.util.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Created by jiangtao on 2017/3/20 11:10
* E-mail:3305727299@qq.com
*/
public class File2Base64 {
/**
* 将文件转成base64 字符串
*
* @param path 文件路径
* @return 编码后的字符串
* @author 3305727299@qq.com
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return Base64.encodeToString(buffer, Base64.DEFAULT);
}
/**
* 将base64字符解码保存文件
*
* @param base64Code 编码后的字串
* @param savePath 文件保存路径
* @author 3305727299@qq.com
*/
public static void decoderBase64File(String base64Code, String savePath) throws Exception {
byte[] buffer = Base64.decode(base64Code, Base64.DEFAULT);
FileOutputStream out = new FileOutputStream(savePath);
out.write(buffer);
out.close();
}
}
网友评论