当前位置: 首页 > ops >正文

附件上传唯一性校验

1. 

    @Overridepublic String uploadFile(MultipartFile file, String id, String funNo, String ctType) {//TODO 附件重复判断// 计算文件哈希值// 将MultipartFile转换为临时File对象String fileHash = "";try {File tempFile = convertMultipartFileToFile(file);// 计算文件哈希值fileHash = calculateFileHash(tempFile);} catch (Exception e) {e.printStackTrace();}TbFile checkFileHash = new TbFile();checkFileHash.setFileHash(fileHash);List<TbFile> checkFileHashList = tbFileMapper.selectTbFileList(checkFileHash);if(checkFileHashList.size()>=1){throw new com.ruoyi.common.exception.CustomException("文件已存在,请勿重复上传!");}// 生成新的唯一文件名并保存文件String uniqueFileName = generateUniqueFileName(file.getName());SysUser user = SecurityUtils.getLoginUser().getUser();// 判断附件类型String extension = "." + FileUploadUtils.getExtension(file);if (!ALLOW_EXCEL_FORM.contains(extension.toLowerCase())) {throw new CustomException("请上传正确的文件类型");}UploadConfig uploadConfig = ApplicationContextUtils.getBean(UploadConfig.class);String shortPath = uploadConfig.getRootUploadPath() + "image/";String[] split = file.getOriginalFilename().split("\\.");String primaryKey = split[0];String wjtype = split[1];String filePath = shortPath + primaryKey + extension;// 临时文件File localTempFile = new File(shortPath + primaryKey + extension);if (!localTempFile.getParentFile().exists()) {localTempFile.getParentFile().mkdir();}if (!localTempFile.exists()) {Try.run(localTempFile::createNewFile).onFailure(e -> log.error("新增文件失败"));}Try.run(() -> file.transferTo(localTempFile)).onFailure(e -> log.error("上传失败"));if (!localTempFile.exists()) {throw new CustomException("上传失败,服务器环境创建文件服务存储的文件夹失败,请联系管理员处理!");}//单附件  先删除,在上传if (funNo.equals("yth_1000") || funNo.equals("yth_1001") || funNo.equals("yth_1003") || funNo.equals("yth_1005") || funNo.equals("yth yth_1006")) {TbFile checkFile = new TbFile();checkFile.setCtIdP(id);checkFile.setCtFunNo(funNo);checkFile.setCtType(ctType);List<TbFile> tbFilesList = tbFileMapper.selectTbFileList(checkFile);for (TbFile tbFile : tbFilesList) {int i = tbFileMapper.deleteTbFileByCtId(tbFile.getCtId());if (i < 1) {throw new CustomException("附件替换失败");}}}TbFile tbFile = new TbFile();tbFile.setCtFileName(primaryKey + "." + wjtype);tbFile.setCtIdP(id);tbFile.setCtFileType(primaryKey);tbFile.setCtFunNo(funNo);tbFile.setCtFileUel(filePath);tbFile.setCtType(ctType);tbFile.setBy5(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getNowDate()));//附件上传时间tbFile.setBy4(String.valueOf(user.getUserId()));//上传附件的用户idtodo  tbFile.setFileHash(fileHash);int i = tbFileMapper.insertTbFile(tbFile);if (i != 1) {throw new CustomException(FunNoConstants.AJK_AJQQ);}return "文件上传成功";}

2. 

    /*** 计算文件的MD5哈希值*/private String calculateFileHash(File file) throws Exception {MessageDigest digest = MessageDigest.getInstance("MD5");try (FileInputStream fis = new FileInputStream(file)) {byte[] buffer = new byte[8192];int read;while ((read = fis.read(buffer)) != -1) {digest.update(buffer, 0, read);}}byte[] hashBytes = digest.digest();StringBuilder sb = new StringBuilder();for (byte b : hashBytes) {sb.append(String.format("%02x", b));}return sb.toString();}/*** 生成唯一文件名*/private String generateUniqueFileName(String originalFileName) {// 实际应用中可能需要更复杂的逻辑// 这里简单地使用时间戳String extension = "";int dotIndex = originalFileName.lastIndexOf('.');if (dotIndex > 0) {extension = originalFileName.substring(dotIndex);}return System.currentTimeMillis() + extension;}/*** 将MultipartFile转换为File对象*/private File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {File tempFile = File.createTempFile("temp-", "-" + multipartFile.getOriginalFilename());try (FileOutputStream fos = new FileOutputStream(tempFile)) {fos.write(multipartFile.getBytes());}// 设置JVM退出时删除临时文件tempFile.deleteOnExit();return tempFile;}

3.数据库

4.测试

http://www.xdnf.cn/news/10032.html

相关文章:

  • 【AI非常道】二零二五年五月,AI非常道
  • Linux研学-入门命令
  • neo4j图数据库
  • 深入了解MCP基础与架构
  • Vue3 + VTable 高性能表格组件完全指南,一个基于 Canvas 的高性能表格组件
  • Go语言通道如何实现通信
  • C文件操作1
  • 基于 AUTOSAR 的域控产品软件开发:从 CP 到 AP 的跨越
  • Java Web 开发详细流程
  • AgenticSeek: 100% 本地替代 Manus AI 的方案
  • Proteus寻找元器件(常见)
  • MFA多因素认证与TOTP算法核心解析(含Java案例)
  • 【产品经理从0到1】平台端产品设计
  • 超级对话2:大跨界且大综合的学问融智学应用场景述评(不同第三方的回应)之二
  • docker部署自动化测试环境笔记
  • Windows 11 家庭版 安装Docker教程
  • einops库介绍(安装/主要函数/应用场景)
  • java.io.IOException: ZIP entry size is too large or invalid
  • OS9.【Linux】基本权限(下)
  • Realsense D435i 使用说明
  • Python+requests+pytest接口自动化测试框架的搭建(全)
  • 在大型中实施访问控制 语言模型
  • Linux-pcie ranges介绍
  • 量化qmt跟单聚宽小市值策略开发成功
  • ESP32-C3 Vscode+ESP-IDF开发环境搭建 保姆级教程
  • DeepSeek‑R1-0528 重磅升级:蚂蚁百宝箱免费、无限量调用
  • k8s容器入门(9)Kubernetes yaml常用配置
  • DeepSeek-R1-0528-Qwen3-8B 本地ollama离线运行使用和llamafactory lora微调
  • 从零打造算法题刷题助手:Agent搭建保姆级攻略
  • Shopify 主题开发:页脚信息架构搭建技巧