陕西网站建设教程江西建设银行分行网站
2026/3/14 22:58:29 网站建设 项目流程
陕西网站建设教程,江西建设银行分行网站,网站建设的技术标准,山东泰安旅游景点大全使用高拍仪拍照#xff0c;生成的图片是base64格式的图片#xff0c;储存到数据库的时候占用的内存太大#xff0c;所以将base64格式储存到本地。下面代码使用的是储存到本地的D:\upload\images\2026\2\2 这个是开发环境#xff0c;如果是放到服务器的话#xff0c;将D:\…使用高拍仪拍照生成的图片是base64格式的图片储存到数据库的时候占用的内存太大所以将base64格式储存到本地。下面代码使用的是储存到本地的D:\upload\images\2026\2\2 这个是开发环境如果是放到服务器的话将D:\upload替换成服务器的磁盘中下面的是代码package org.jeecg.modules.business.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.util.Base64; /** * 将Base64格式图片保存到本地磁盘的工具类 */ Slf4j Component public class Base64ImageSaver { // 目标存储目录 D:/upload Value(value ${jeecg.path.upload}) private String uploadPath; /** * 将Base64图片保存到本地 * * param base64Image Base64格式的图片字符串可带前缀 * param type 传过来的照片类型 * return 保存成功返回文件完整路径失败返回null */ public String saveBase64ImageToLocal(String base64Image, Integer type) { // 1. 校验入参 if (base64Image null || base64Image.isEmpty() || type null ) { log.error(Base64字符串或文件名不能为空); return null; } String newFileName ; if (type 999){ newFileName 办理人照片_System.currentTimeMillis(); }else{ newFileName _System.currentTimeMillis(); } // 2. 拼接日期目录路径优化路径拼接避免手动拼接斜杠 LocalDateTime nowDateTime LocalDateTime.now(); String datePath String.format(images/%d/%d/%d, nowDateTime.getYear(), nowDateTime.getMonthValue(), nowDateTime.getDayOfMonth()); // 完整的目录路径 File dirFile new File(uploadPath, datePath); // 创建目录递归创建无需判断是否存在mkdirs()已处理 if (!dirFile.exists()) { boolean mkdirSuccess dirFile.mkdirs(); if (!mkdirSuccess) { log.error(创建目录失败{}, dirFile.getAbsolutePath()); return null; } } // 3. 处理Base64前缀提取纯编码数据 String pureBase64Data base64Image; if (base64Image.contains(;base64,)) { pureBase64Data base64Image.split(;base64,)[1]; } // 4. 拼接完整的文件路径目录文件名 File targetFile new File(dirFile, newFileName.png); // 5. 解码并写入文件 try (FileOutputStream outputStream new FileOutputStream(targetFile)) { // 解码Base64为字节数组 byte[] imageBytes Base64.getDecoder().decode(pureBase64Data); // 写入文件 outputStream.write(imageBytes); outputStream.flush(); String filePath targetFile.getAbsolutePath(); log.info(图片保存成功{}, filePath); String result filePath.replaceFirst(^.*upload\\\\, ).replace(\\, /); return result; } catch (IllegalArgumentException e) { log.error(Base64解码失败{}, e.getMessage()); return null; } catch (IOException e) { log.error(文件写入失败{}路径{}, e.getMessage(), targetFile.getAbsolutePath()); return null; } } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询