如果涉及到数据库表的某个字段存二进制文件,然后接口再拿到二进制后可以转成base64码,然后就可以通过以下生成指定目录下了。什么文件都行,.txt,.png,.ppt,.pdf,.mp4等
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;public class Demo {public static void main(String[] args) {// 图片文件路径//String imagePath = "D:\\image\\fkxqs.jpg"; // 请确认文件路径String imagePath = "D:\\image\\test.ppt"; // 请确认文件路径String base64String ="";try {// 读取图片文件为字节数组File file = new File(imagePath);byte[] fileContent = Files.readAllBytes(file.toPath());// 将字节数组转换为Base64编码base64String = Base64.getEncoder().encodeToString(fileContent);// 输出Base64字符串System.out.println("Base64编码:");System.out.println(base64String);} catch (IOException e) {e.printStackTrace();}// 解码Base64字符串byte[] imageBytes = Base64.getDecoder().decode(base64String);// 保存图片到D盘home文件夹String toPath = "D:\\home\\test.ppt"; // 根据需要修改文件名和格式try (FileOutputStream fos = new FileOutputStream(toPath)) {fos.write(imageBytes);System.out.println("图片已保存到: " + toPath);} catch (IOException e) {e.printStackTrace();}}}