diff --git a/bonus-business/src/main/java/com/bonus/business/service/impl/TbPromotionMaterialServiceImpl.java b/bonus-business/src/main/java/com/bonus/business/service/impl/TbPromotionMaterialServiceImpl.java index f952a66..5fd332c 100644 --- a/bonus-business/src/main/java/com/bonus/business/service/impl/TbPromotionMaterialServiceImpl.java +++ b/bonus-business/src/main/java/com/bonus/business/service/impl/TbPromotionMaterialServiceImpl.java @@ -27,7 +27,7 @@ import javax.annotation.Resource; /** * 宣传物料信息Service业务层处理 - * + * * @author 黑子 * @date 2025-09-10 */ @@ -54,7 +54,7 @@ public class TbPromotionMaterialServiceImpl implements ITbPromotionMaterialServi /** * 查询宣传物料信息列表 - * + * * @param tbPromotionMaterial 宣传物料信息 * @return 宣传物料信息 */ @@ -103,7 +103,7 @@ public class TbPromotionMaterialServiceImpl implements ITbPromotionMaterialServi /** * 新增宣传物料信息 - * + * * @param tbPromotionMaterial 宣传物料信息 * @return 结果 */ @@ -113,7 +113,7 @@ public class TbPromotionMaterialServiceImpl implements ITbPromotionMaterialServi tbPromotionMaterial.setCreateUser(SecurityUtils.getUserId()); tbPromotionMaterial.setCreateTime(DateUtils.getNowDate()); int i = mapper.insertTbPromotionMaterial(tbPromotionMaterial); - if (i > 0) { + if (tbPromotionMaterial.getProductId() != null && !tbPromotionMaterial.getProductId().isEmpty() && i > 0) { //将产品与物料关系存到另一张表 List split = tbPromotionMaterial.split(tbPromotionMaterial); int n = mapper.insertMaterialProductRelevance(split); @@ -125,7 +125,7 @@ public class TbPromotionMaterialServiceImpl implements ITbPromotionMaterialServi /** * 修改宣传物料信息 - * + * * @param tbPromotionMaterial 宣传物料信息 * @return 结果 */ @@ -135,7 +135,7 @@ public class TbPromotionMaterialServiceImpl implements ITbPromotionMaterialServi tbPromotionMaterial.setUpdateUser(SecurityUtils.getUserId()); tbPromotionMaterial.setUpdateTime(DateUtils.getNowDate()); int i = mapper.updateTbPromotionMaterial(tbPromotionMaterial); - if (i > 0) { + if (tbPromotionMaterial.getProductId() != null && !tbPromotionMaterial.getProductId().isEmpty() && i > 0) { //删除以前的关联关系 int x = mapper.deleteMaterialProductRelevance(tbPromotionMaterial.getId()); //将产品与物料关系存到另一张表 @@ -160,7 +160,7 @@ public class TbPromotionMaterialServiceImpl implements ITbPromotionMaterialServi /** * 删除宣传物料信息信息 - * + * * @param id 宣传物料信息主键 * @return 结果 */ diff --git a/bonus-business/src/main/resources/mapper/business/TbPromotionMaterialMapper.xml b/bonus-business/src/main/resources/mapper/business/TbPromotionMaterialMapper.xml index 78efa1d..2027a70 100644 --- a/bonus-business/src/main/resources/mapper/business/TbPromotionMaterialMapper.xml +++ b/bonus-business/src/main/resources/mapper/business/TbPromotionMaterialMapper.xml @@ -3,7 +3,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + @@ -29,13 +29,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" from tb_promotion_material aaa left join sys_user su on su.user_id = aaa.create_user where aaa.del_flag = 0 - - and aaa.name like concat('%', #{name}, '%') - and aaa.type_id = #{typeId} - and aaa.type_name like concat('%', #{typeName}, '%') - + and aaa.name like concat('%', #{name}, '%') + and aaa.type_id = #{typeId} + and aaa.type_name like concat('%', #{typeName}, '%') - + - select product_id from tb_promotion_material_product where material_id = #{id} + select product_id from tb_promotion_material_product where material_id = #{id} limit 1 - \ No newline at end of file + diff --git a/bonus-common/src/main/java/com/bonus/common/utils/file/FileTypeUtils.java b/bonus-common/src/main/java/com/bonus/common/utils/file/FileTypeUtils.java index ee78a6c..f57f0d3 100644 --- a/bonus-common/src/main/java/com/bonus/common/utils/file/FileTypeUtils.java +++ b/bonus-common/src/main/java/com/bonus/common/utils/file/FileTypeUtils.java @@ -3,6 +3,8 @@ package com.bonus.common.utils.file; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.springframework.web.multipart.MultipartFile; @@ -15,39 +17,31 @@ import org.springframework.web.multipart.MultipartFile; public class FileTypeUtils { - // 常见视频文件的文件头(十六进制) - private static final String[] VIDEO_HEADER_PREFIXES = { - "000001BA", // MPEG - "000001B3", // MPEG - "66747970", // MP4 - "464C56", // FLV - "494433", // MP3 (音频,可排除) - "89504E47", // PNG (图片) - "47494638", // GIF - "25504446", // PDF - "FFD8FFE0", // JPEG - "52494646", // WebP / AVI (RIFF 容器) - "4D5A" // EXE (危险文件) - }; + // 常见的视频文件扩展名(小写) + private static final Set VIDEO_EXTENSIONS = new HashSet<>(); - // 真正的视频 MIME 类型 - private static final String[] VIDEO_CONTENT_TYPES = { - "video/mp4", - "video/avi", - "video/x-msvideo", - "video/mpeg", - "video/quicktime", - "video/x-flv", - "video/webm", - "video/3gpp", - "video/ogg" - }; + static { + VIDEO_EXTENSIONS.add("mp4"); + VIDEO_EXTENSIONS.add("avi"); + VIDEO_EXTENSIONS.add("mkv"); + VIDEO_EXTENSIONS.add("mov"); + VIDEO_EXTENSIONS.add("wmv"); + VIDEO_EXTENSIONS.add("flv"); + VIDEO_EXTENSIONS.add("rmvb"); + VIDEO_EXTENSIONS.add("3gp"); + VIDEO_EXTENSIONS.add("webm"); + VIDEO_EXTENSIONS.add("m4v"); + VIDEO_EXTENSIONS.add("mpg"); + VIDEO_EXTENSIONS.add("mpeg"); + VIDEO_EXTENSIONS.add("vob"); + VIDEO_EXTENSIONS.add("ogv"); + } /** * 获取文件类型 *

* 例如: bonus.txt, 返回: txt - * + * * @param file 文件名 * @return 后缀(不含".") */ @@ -80,7 +74,7 @@ public class FileTypeUtils /** * 获取文件类型 - * + * * @param photoByte 文件字节码 * @return 后缀(不含".") */ @@ -108,70 +102,27 @@ public class FileTypeUtils } /** - * 判断 MultipartFile 是否为视频文件(Java 8 兼容版) + * 判断 MultipartFile 是否为视频文件(基于后缀名) + * + * @param file 上传的文件 + * @return true 表示是视频文件,false 不是 */ public static boolean isVideoFile(MultipartFile file) { if (file == null || file.isEmpty()) { return false; } - String contentType = file.getContentType(); - if (contentType == null) { + String fileName = file.getOriginalFilename(); + if (fileName == null || fileName.trim().isEmpty()) { return false; } - // 先用 MIME 类型快速判断(备用方案) - boolean isVideoByMimeType = Arrays.stream(VIDEO_CONTENT_TYPES) - .anyMatch(contentType::startsWith); - try { - byte[] headerBytes = new byte[12]; - int read = file.getInputStream().read(headerBytes); - if (read < 4) { - return false; - } - String headerHex = bytesToHex(headerBytes, Math.min(read, 8)).toUpperCase(); - boolean isVideo = false; // 用一个变量保存结果 - switch (headerHex) { - case "66747970": // MP4 - case "000001BA": // MPEG - case "000001B3": // MPEG - case "464C56": // FLV - isVideo = true; - break; - case "52494646": // RIFF (AVI, WebM) - // 进一步判断子类型:AVI 或 WebM - if (read >= 12) { - String subType = bytesToHex(Arrays.copyOfRange(headerBytes, 8, 12)).toUpperCase(); - isVideo = "41564920".equals(subType) || "5745424D".equals(subType); // "AVI " 或 "WEBM" - } - break; - - default: - // 文件头不匹配,降级使用 MIME 类型判断 - isVideo = isVideoByMimeType; - break; - } - return isVideo; - } catch (IOException e) { - // 读取出错,降级使用 MIME 类型判断 - return isVideoByMimeType; + // 获取最后一个点的位置(防止文件名含多个点) + int lastDotIndex = fileName.lastIndexOf('.'); + if (lastDotIndex < 0 || lastDotIndex == fileName.length() - 1) { + return false; // 没有后缀名或以.结尾 } + // 提取后缀名并转小写 + String extension = fileName.substring(lastDotIndex + 1).toLowerCase(); + return VIDEO_EXTENSIONS.contains(extension); } - /** - * 将字节数组转为十六进制字符串 - */ - private static String bytesToHex(byte[] bytes, int len) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < len; i++) { - sb.append(String.format("%02X", bytes[i] & 0xFF)); - } - return sb.toString(); - } - - /** - * 获取字节数组的十六进制表示(完整) - */ - private static String bytesToHex(byte[] bytes) { - return bytesToHex(bytes, bytes.length); - } - -} \ No newline at end of file +}