From 220098e947b36d420aec3f7f5d42a7c828dc3135 Mon Sep 17 00:00:00 2001 From: zhangjinli Date: Wed, 6 Jul 2022 15:38:48 +0800 Subject: [PATCH] zh --- .../controller/UtilsController.java | 81 ++++++++++++------- .../com/zh/project0512/model/Reference.java | 13 ++- src/main/resources/application-dev.yml | 5 ++ src/main/resources/application-test.yml | 7 +- src/main/resources/mapper/ReferenceMapper.xml | 1 + 5 files changed, 73 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/zh/project0512/controller/UtilsController.java b/src/main/java/com/zh/project0512/controller/UtilsController.java index 77dff34..5c14da0 100644 --- a/src/main/java/com/zh/project0512/controller/UtilsController.java +++ b/src/main/java/com/zh/project0512/controller/UtilsController.java @@ -2,45 +2,32 @@ package com.zh.project0512.controller; import com.alibaba.fastjson.JSONObject; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; //import com.zh.project0512.service.IUserService; -import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.zh.project0512.mapper.QywxDepartmentUserLinkMapper; import com.zh.project0512.mapper.RankMapper; -import com.zh.project0512.mapper.UserMapper; -import com.zh.project0512.model.Rank; -import com.zh.project0512.model.Task; -import com.zh.project0512.model.UserPointsRecords; -import com.zh.project0512.model.Video; -import com.zh.project0512.model.dto.RankListDTO; -import com.zh.project0512.model.vo.RankListVo; -import com.zh.project0512.service.IRewardRuleService; import com.zh.project0512.service.ITaskService; -import com.zh.project0512.service.IUserPointsRecordsService; import com.zh.project0512.service.IVideoService; -import com.zh.project0512.utils.AliyunOss; import com.zh.project0512.utils.FileTypeUtil; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import org.jsoup.helper.StringUtil; +import net.coobird.thumbnailator.Thumbnails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import ws.schild.jave.MultimediaObject; import javax.annotation.Resource; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.File; +import java.io.IOException; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -59,26 +46,33 @@ import java.util.stream.Collectors; @RequestMapping("/util") public class UtilsController { - - @Autowired - private RedisTemplate redisTemplate; - @Autowired - private AliyunOss aliyunOss; +// @Autowired +// private RedisTemplate redisTemplate; +// @Autowired +// private AliyunOss aliyunOss; @Value("${web.uploadPath}") private String path; + @Value("${web.picSizeLimit}") + private long picSizeLimit; @Value("${web.cdn}") private String cdn; @Operation(summary = "上传") @PostMapping("/upload") @ResponseBody - public Result upload(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name) { + public Result upload(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name, Boolean compress) { if (file.isEmpty()) { return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "请选择文件"); } String fileName = file.getOriginalFilename();//文件名 - String newFileName = UUID.randomUUID().toString().replace("-", "") + LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli() + fileName.substring(fileName.lastIndexOf(".")); + String randomName = UUID.randomUUID().toString().replace("-", "") + LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); + String newFileName = randomName + fileName.substring(fileName.lastIndexOf(".")); + String newFileNameSmall = randomName + "Small" + fileName.substring(fileName.lastIndexOf(".")); + + JSONObject res = new JSONObject(); + int fileTypeNum = FileTypeUtil.getcontentTypeNum((fileName.substring(fileName.lastIndexOf(".")))); + //String path = System.getProperty("user.home"); //文件存储位置 我放在了我的项目下 // //获取jar包所在目录 // ApplicationHome h = new ApplicationHome(getClass()); @@ -88,14 +82,23 @@ public class UtilsController { // System.out.println(dirPath); // System.out.println(path); // File dest = new File(dirPath+fileName); - File dest = new File(path + "/" + newFileName); - if (!dest.getParentFile().exists()) { - dest.getParentFile().mkdirs(); - } try { + String filePath = path + "/" + newFileName; + File dest = new File(filePath); + if (compress != null && compress &&fileTypeNum ==2&& file.getSize() >= picSizeLimit) { + String filePathSmall = path + "/" + newFileNameSmall; + File beforeFile = new File(filePathSmall); + //生成目标图片 + Thumbnails.of(file.getInputStream()).scale(1f).toFile(beforeFile); + //压缩图片至指定大小下 + commpressPicCycle(filePathSmall, picSizeLimit, 0.8); + res.put("fileUrlSmall", "/upload/" + newFileNameSmall); + } +// if (!dest.getParentFile().exists()) { +// dest.getParentFile().mkdirs(); +// } file.transferTo(dest); - JSONObject res = new JSONObject(); - int fileTypeNum = FileTypeUtil.getcontentTypeNum((fileName.substring(fileName.lastIndexOf(".")))); + res.put("fileUrl", "/upload/" + newFileName); res.put("fileName", fileName); res.put("fileType", FileTypeUtil.getcontentType((fileName.substring(fileName.lastIndexOf("."))))); @@ -156,6 +159,20 @@ public class UtilsController { return res; } + private static void commpressPicCycle(String desPath, long desFileSize, double accuracy) throws IOException { + File srcFileJPG = new File(desPath); + //如果小于指定大小不压缩;如果大于等于指定大小压缩 + if (srcFileJPG.length() <= desFileSize) { + return; + } + // 计算宽高 + BufferedImage bim = ImageIO.read(srcFileJPG); + int desWidth = new BigDecimal(bim.getWidth()).multiply(new BigDecimal(accuracy)).intValue(); + int desHeight = new BigDecimal(bim.getHeight()).multiply(new BigDecimal(accuracy)).intValue(); + Thumbnails.of(desPath).size(desWidth, desHeight).outputQuality(accuracy).toFile(desPath); + commpressPicCycle(desPath, desFileSize, accuracy); + } + @Autowired private ITaskService taskService; @Resource @@ -190,6 +207,7 @@ public class UtilsController { // int b = StringUtils.countOccurrencesOf(a, "https://v.douyin"); // System.out.println(b); + List list1 = new ArrayList(); list1.add("1"); list1.add("2"); @@ -228,4 +246,7 @@ public class UtilsController { System.out.println("---原来的List2---"); list2.parallelStream().forEachOrdered(System.out::println); } + + private class LogUtil { + } } diff --git a/src/main/java/com/zh/project0512/model/Reference.java b/src/main/java/com/zh/project0512/model/Reference.java index d010f10..4302904 100644 --- a/src/main/java/com/zh/project0512/model/Reference.java +++ b/src/main/java/com/zh/project0512/model/Reference.java @@ -43,7 +43,7 @@ public class Reference extends Model { /** * 素材分组id(0为未分组) */ - @Schema(title = "素材分组id",description = "0为未分组") + @Schema(title = "素材分组id", description = "0为未分组") @TableField("groupId") private Integer groupId; @@ -54,7 +54,7 @@ public class Reference extends Model { /** * 类型:1视频;2图片 */ - @Schema(title = "类型",description = "1视频;2图片") + @Schema(title = "类型", description = "1视频;2图片") private Integer type; /** @@ -65,6 +65,13 @@ public class Reference extends Model { @TableField("fileUrl") private String fileUrl; + /** + * 文件缩略图 + */ + @Schema(title = "文件缩略图(图片超高清时方便预加载)") + @TableField("fileUrlSmall") + private String fileUrlSmall; + /** * 时长(视频需要) */ @@ -75,7 +82,7 @@ public class Reference extends Model { /** * 封面图片地址 */ - @Schema(title = "封面图片地址",description = "视频文件请上传封面图片") + @Schema(title = "封面图片地址", description = "视频文件请上传封面图片") @TableField("coverUrl") private String coverUrl; diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index dfeae61..f8c75aa 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -53,7 +53,12 @@ mybatis-plus: logic-delete-value: 0 web: apiPath: com.zh.project0512.controller + # mac图片地址 +# uploadPath: /Users/apple/Pictures + # 线上图片上传位置 uploadPath: /www/wwwroot/project0512/upload/ + # 图片压缩尺寸界限(1024*1024) + picSizeLimit: 1048576 cdn: http://cdn.sws010.com #springdoc: diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index 02cf94b..6f0f5bc 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -53,7 +53,12 @@ mybatis-plus: logic-delete-value: 0 web: apiPath: com.zh.project0512.controller - uploadPath: /static/sws/upload/ + # mac图片地址 +# uploadPath: /Users/apple/Pictures + # 线上图片上传位置 + uploadPath: /www/wwwroot/project0512/upload/ + # 图片压缩尺寸界限(1024*1024) + picSizeLimit: 1048576 cdn: http://cdn.sws010.com #springdoc: diff --git a/src/main/resources/mapper/ReferenceMapper.xml b/src/main/resources/mapper/ReferenceMapper.xml index 668c6ce..541f661 100644 --- a/src/main/resources/mapper/ReferenceMapper.xml +++ b/src/main/resources/mapper/ReferenceMapper.xml @@ -54,6 +54,7 @@ +