@[TOC]
File转成Base64编码
/**
* 文件转成base64编码
*
* @return base64编码
*/
private static String fileToBase64() {
File file = new File("F:/111.jpg");
if (!file.exists()) {
return null;
}
// base64编码
String context = null;
try {
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
context = Base64.getEncoder().encodeToString(buffer).replaceAll("\r|\n", "");
} catch (Exception e) {
e.printStackTrace();
}
return context;
}
Base64编码转成File
/**
* base64转成文件并保存本地
*
* @param fileContext 文件base64编码
* @param fileType 文件类型后缀(jpg、png)
*/
private static void base64ToFile(String fileContext, String fileType) {
// 判断文件base64编码是否为空
if (Strings.isBlank(fileContext)) {
return;
}
// 设置文件存储位置
final String path = "D:/images/" + fileStorageLocation(fileType);
final Base64.Decoder decoder = Base64.getDecoder();
// 生成文件
File file = new File(path);
// 创建文件及目录【这里使用hutool的方法】
FileUtil.touch(file);
try (OutputStream out = new FileOutputStream(file)) {
out.write(decoder.decode(fileContext));
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据当前年、月、日设置文件存储位置
*
* @param fileType 文件类型
* @return 文件存储位置【返回示例:2022/09/05/6a3297cb-c189-44ca-a57e-3229ac6e2f83.png】
*/
private static String fileStorageLocation(String fileType) {
StringJoiner joiner = new StringJoiner("/");
joiner.add(getCurrentYear());
joiner.add(getCurrentMonth());
joiner.add(getCurrentDay());
joiner.add(UUID.randomUUID().toString() + "." + fileType);
return joiner.toString();
}
/**
* 获取当前年
*/
private static String getCurrentYear() {
return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
}
/**
* 获取当前月【前面补0】
*/
private static String getCurrentMonth() {
final int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
if (month < 10) {
return "0" + month;
}
return String.valueOf(month);
}
/**
* 获取当前日【前面补0】
*/
private static String getCurrentDay() {
final int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
if (day < 10) {
return "0" + day;
}
return String.valueOf(day);
}
File转成Base64编码 Base64编码转成File【全代码】
import cn.hutool.core.io.FileUtil;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.util.Strings;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Calendar;
import java.util.StringJoiner;
import java.util.UUID;
public class FileUtils {
public static void main(String[] args) {
// 文件转成base64编码
final FileDTO fileDTO = fileToBase64();
// base编码转成文件并保存本地
base64ToFile(fileDTO.getContext(), fileDTO.getType());
}
/**
* 文件转成base64编码
*
* @return
*/
private static FileDTO fileToBase64() {
File file = new File("F:/111.jpg");
if (!file.exists()) {
return null;
}
// base64编码
String context = null;
try {
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
context = Base64.getEncoder().encodeToString(buffer).replaceAll("\r|\n", "");
} catch (Exception e) {
e.printStackTrace();
}
return FileDTO.builder()
.type(file.getName().split("\\.")[1])
.context(context)
.build();
}
/**
* base64转成文件并保存
*
* @param fileContext 文件base64编码
* @param fileType 文件类型后缀(jpg、png)
*/
private static void base64ToFile(String fileContext, String fileType) {
// 判断文件base64编码是否为空
if (Strings.isBlank(fileContext)) {
return;
}
// 设置文件存储位置
final String path = "D:/images/" + fileStorageLocation(fileType);
final Base64.Decoder decoder = Base64.getDecoder();
// 生成文件
File file = new File(path);
// 创建文件及其父目录【这里使用hutool的方法】
FileUtil.touch(file);
try (OutputStream out = new FileOutputStream(file)) {
out.write(decoder.decode(fileContext));
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据当前年、月、日设置文件存储位置
*
* @param fileType 文件类型
* @return 文件存储位置【返回示例:2022/09/05/6a3297cb-c189-44ca-a57e-3229ac6e2f83.png】
*/
private static String fileStorageLocation(String fileType) {
StringJoiner joiner = new StringJoiner("/");
joiner.add(getCurrentYear());
joiner.add(getCurrentMonth());
joiner.add(getCurrentDay());
joiner.add(UUID.randomUUID().toString() + "." + fileType);
return joiner.toString();
}
/**
* 获取当前年
*/
private static String getCurrentYear() {
return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
}
/**
* 获取当前月【前面补0】
*/
private static String getCurrentMonth() {
final int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
if (month < 10) {
return "0" + month;
}
return String.valueOf(month);
}
/**
* 获取当前日【前面补0】
*/
private static String getCurrentDay() {
final int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
if (day < 10) {
return "0" + day;
}
return String.valueOf(day);
}
@Setter
@Getter
@Builder
private static class FileDTO {
// 文件类型
private String type;
// 文件base64编码
private String context;
}
}
End