代码如下,有基本注释,直接复制过去,可能需要导包
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
//图片文件转Base64编码
public class FileCode {
/**
* <p>将文件转成base64 字符串</p>
* @param path 文件路径
*/
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 new BASE64Encoder().encode(buffer);
}
/**
* <p>将base64字符解码保存文件</p>
*/
public static void decoderBase64File(String base64Code,String targetPath) throws Except