Java 图片、文件 Base64 互转
package com.thoth.his.base.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
public class ImageUtil {
public String FileToBase64(String filePath) throws IOException {
FileInputStream inputStream = null;
try {
Base64.Encoder encoder = Base64.getEncoder();
inputStream = new FileInputStream(filePath);
int available = inputStream.available();
byte[] bytes = new byte[available];
inputStream.read(bytes);
return encoder.encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
inputStream.close();
}
return "";
}
public boolean Base64ToFile(String base64Str, String saveFilePath) throws IOException {
FileOutputStream outputStream = null;
try {
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(base64Str);
outputStream = new FileOutputStream(saveFilePath);
outputStream.write(bytes);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStream.close();
}
return false;
}
}
标签:Java,String,Base64,bytes,inputStream,import,互转,java
From: https://www.cnblogs.com/vipsoft/p/17611420.html