import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Scanner;
/**
* @Author: dell
* @Version: V1.0.0
* @Date: 2022年04月27日
*/
public class ImgToBase64 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String path = sc.next();
String base64String = LocalImage(path);
System.out.println("[图片转换Base64]=" + base64String);
}
/**
* 图片路径
* @param imgPath
* @return
*/
private static String LocalImage(String imgPath) {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
String base64String = new String(Base64.encode(Objects.requireNonNull(data)));
return base64String;
}
}
传入图片路径,对图片进行base64编码
最新推荐文章于 2024-10-10 11:12:49 发布