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 static 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 static 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;
}
}
今天给大家分享一个SpringBoot整合Tess4j库实现图片文字识别的小案例,希望xdm喜欢。文末有案例代码的Git地址,可以自己下载了去玩玩儿或继续扩展也行。