package com.chenwc.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;
public class Utils {
private final static Logger log = LoggerFactory.getLogger(Utils.class);
public static void main(String[] args) {
// TODO 自动生成的方法存根
File file = new File("D:\\Downloads\\微信图片_20231018152634.jpg");
String encodeImageData = imageToBase64Str(file);
//log.info(encodeImageData);
String encodeImageData2 = convertImageToBase64Str(file);
//log.info(encodeImageData2);
log.info(String.valueOf(encodeImageData2.equals(encodeImageData)));
base64StrToImage(encodeImageData, "D:\\Downloads\\111.jpg");
convertBase64StrToImage(encodeImageData, "D:\\Downloads\\222.jpg");
}
/**
* 图片转Base64字符串
*
* @param imageFile 图片文件
* @return Base64字符串
*/
public static String convertImageToBase64Str(File imageFile) {
try {
long start = System.currentTimeMillis();
String ret = Base64.getEncoder().encodeToString(Files.readAllBytes(imageFile.toPath()));
long end = System.currentTimeMillis();
log.info("图片文件转 Base64 字符串转换总用时:{} ms", end - start);
return ret;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Base64字符串转图片
*
* @param path 图片文件保存路径
* @param string Base64字符串
*/
public static void convertBase64StrToImage(String string, String path) {
try {
File file = new File(path);
log.info("写入文件路径: {}", file.getAbsolutePath());
if (!file.exists()) {
mkdirParents(file);
if (!file.createNewFile()) {
log.info("文件不存在,创建文件失败!");
return;
} else {
log.info("文件不存在,创建文件成功!");
}
}
long start = System.currentTimeMillis();
log.info("开始写入文件............");
Files.write(file.toPath(), Base64.getDecoder().decode(string));
long end = System.currentTimeMillis();
log.info("写入文件成功............");
log.info("写入文件耗时: {} ms", end - start);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Base64字符串转图片
*
* @param string Base64字符串
* @param path 图片文件保存路径
*/
public static void base64StrToImage(String string, String path) {
FileOutputStream fos = null;
File file = new File(path);
log.info("写入文件路径: {}", file.getAbsolutePath());
try {
if (!file.exists()) {
mkdirParents(file);
if (!file.createNewFile()) {
log.info("文件不存在,创建文件失败!");
return;
} else {
log.info("文件不存在,创建文件成功!");
}
}
long start = System.currentTimeMillis();
log.info("开始写入文件............");
fos = new FileOutputStream(file);
fos.write(Base64.getDecoder().decode(string));
long end = System.currentTimeMillis();
log.info("写入文件成功............");
log.info("写入文件耗时: {} ms", end - start);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fos) {
fos.close();
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
/**
* 图片文件转换为base64字符串
*
* @param file 图片文件
* @return base64字符串
*/
public static String imageToBase64Str(File file) {
byte[] result = null;
long start = System.currentTimeMillis();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int available = fis.available();
result = new byte[available];
fis.read(result);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
try {
if (null != fis) {
fis.close();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
String ret = Base64.getEncoder().encodeToString(result);
long end = System.currentTimeMillis();
log.info("图片文件转 Base64 字符串转换总用时:{} ms", end - start);
return ret;
}
/**
* 判断文件的父级目录是否存在,不存在则创建
*
* @param file 文件
* @return true 父级目录存在或创建父级目录成功, false创建父级目录失败
*/
private static boolean mkdirParents(File file) {
if (!file.getParentFile().exists()) {
return file.getParentFile().mkdirs();
} else {
return true;
}
}
}