|
|
|
|
package com.zh.project0512.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
//import com.zh.project0512.service.IUserService;
|
|
|
|
|
import com.zh.project0512.mapper.QywxDepartmentUserLinkMapper;
|
|
|
|
|
import com.zh.project0512.mapper.RankMapper;
|
|
|
|
|
import com.zh.project0512.service.ITaskService;
|
|
|
|
|
import com.zh.project0512.service.IVideoService;
|
|
|
|
|
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 net.coobird.thumbnailator.Thumbnails;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
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.util.*;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* 前端控制器
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* @author zh
|
|
|
|
|
* @since 2022-05-12
|
|
|
|
|
*/
|
|
|
|
|
@Tag(name = "工具中心")
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/util")
|
|
|
|
|
public class UtilsController {
|
|
|
|
|
|
|
|
|
|
// @Autowired
|
|
|
|
|
// private RedisTemplate<String, String> 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, Boolean compress) {
|
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
|
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "请选择文件");
|
|
|
|
|
}
|
|
|
|
|
String fileName = file.getOriginalFilename();//文件名
|
|
|
|
|
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());
|
|
|
|
|
// File jarF = h.getSource();
|
|
|
|
|
// //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
|
|
|
|
|
// String dirPath = jarF.getParentFile().toString()+"/upload/";
|
|
|
|
|
// System.out.println(dirPath);
|
|
|
|
|
// System.out.println(path);
|
|
|
|
|
// File dest = new File(dirPath+fileName);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
res.put("fileUrl", "/upload/" + newFileName);
|
|
|
|
|
res.put("fileName", fileName);
|
|
|
|
|
res.put("fileType", FileTypeUtil.getcontentType((fileName.substring(fileName.lastIndexOf(".")))));
|
|
|
|
|
res.put("fileTypeNum", fileTypeNum);
|
|
|
|
|
if (fileTypeNum == 1) {
|
|
|
|
|
res.put("duration", new MultimediaObject(dest).getInfo().getDuration());
|
|
|
|
|
}
|
|
|
|
|
return Result.success(res, "上传完成");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "上传失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @PostMapping("/uploadImg")
|
|
|
|
|
// public Result uploadImg(@RequestParam("file") MultipartFile file, @RequestParam(value = "name", defaultValue = "name") String name) {
|
|
|
|
|
// try {
|
|
|
|
|
// String homeImage = aliyunOss.checkImage(file);//此处是调用上传服务接口
|
|
|
|
|
// String fileUrl = aliyunOss.getImgUrl(homeImage);
|
|
|
|
|
// String fileType = FileTypeUtil.getcontentType((fileUrl.substring(fileUrl.lastIndexOf("."))));
|
|
|
|
|
// System.out.println(fileType == "video/mp4" ? 1 : (fileType == "image/jpg" ? 2 : (fileType == "text/plain" ? 3 : 0)));
|
|
|
|
|
// Result result = Result.success("上传成功!");
|
|
|
|
|
// return result;
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// return Result.fail(HttpStatusEnum.NOT_FOUND);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "编辑器内上传")
|
|
|
|
|
@PostMapping("/upload2")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public JSONObject upload2(@RequestParam("file") MultipartFile file, @RequestParam(required = false) String name) {
|
|
|
|
|
JSONObject res = new JSONObject();
|
|
|
|
|
res.put("errno", 1);
|
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
|
res.put("message", "请选择文件");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
String fileName = file.getOriginalFilename();//文件名
|
|
|
|
|
String newFileName = UUID.randomUUID().toString().replace("-", "") + LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli() + fileName.substring(fileName.lastIndexOf("."));
|
|
|
|
|
File dest = new File(path + "/" + newFileName);
|
|
|
|
|
if (!dest.getParentFile().exists()) {
|
|
|
|
|
dest.getParentFile().mkdirs();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
file.transferTo(dest);
|
|
|
|
|
res.replace("errno", 0);
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
|
obj.put("alt", fileName);
|
|
|
|
|
obj.put("url", cdn + "/upload/" + newFileName);
|
|
|
|
|
res.put("data", obj);
|
|
|
|
|
return res;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
res.put("message", "上传失败");
|
|
|
|
|
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
|
|
|
|
|
QywxDepartmentUserLinkMapper qywxDepartmentUserLinkMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
RankMapper rankMapper;
|
|
|
|
|
@Autowired
|
|
|
|
|
private IVideoService videoService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/test")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public Result test() {
|
|
|
|
|
String url = "http://www.baidu.com/#wd=%E6%88%91%E6%98%AF%E6%AD%8C%E6%89%8B&rsv_bp=0&tn=baidu&rsv_spt=3&ie=utf-8&rsv_sug3=11&rsv_sug4=643&rsv_sug1=11&inputT=5484&rsv_sug2=0";
|
|
|
|
|
Pattern p = Pattern.compile("(http://|https://)?([^/]*)", Pattern.CASE_INSENSITIVE);
|
|
|
|
|
|
|
|
|
|
Matcher m = p.matcher(url);
|
|
|
|
|
|
|
|
|
|
String a = m.find() ? m.group(2) : url;
|
|
|
|
|
// (https://)+\S*\s
|
|
|
|
|
return Result.success(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
// String url = "9.20 tRK:/ 男人第一天上班就被当成修电脑的,谁知他竟然是总经理%好剧推荐 %全集解说%幸福一家人%关于爱情%宅家dou剧 %持续关注持续精彩 https://v.kuaishou.com/Yh4DwM9/ 复制此链接,打开Dou音搜索,直接观看视频!";
|
|
|
|
|
// Pattern p = Pattern.compile("((https://)+\\S*\\s)", Pattern.CASE_INSENSITIVE);
|
|
|
|
|
//
|
|
|
|
|
// Matcher m = p.matcher(url);
|
|
|
|
|
// System.out.println(m);
|
|
|
|
|
// String a = m.find() ? m.group(1) : url;
|
|
|
|
|
// System.out.println(a);
|
|
|
|
|
// int b = StringUtils.countOccurrencesOf(a, "https://v.douyin");
|
|
|
|
|
// System.out.println(b);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> list1 = new ArrayList<String>();
|
|
|
|
|
list1.add("1");
|
|
|
|
|
list1.add("2");
|
|
|
|
|
list1.add("3");
|
|
|
|
|
list1.add("5");
|
|
|
|
|
list1.add("6");
|
|
|
|
|
List<String> list2 = new ArrayList<String>();
|
|
|
|
|
list2.add("2");
|
|
|
|
|
list2.add("3");
|
|
|
|
|
list2.add("7");
|
|
|
|
|
list2.add("8");
|
|
|
|
|
// 交集
|
|
|
|
|
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
|
|
|
|
|
System.out.println("---交集 intersection---");
|
|
|
|
|
intersection.parallelStream().forEach(System.out::println);
|
|
|
|
|
// 差集 (list1 - list2)
|
|
|
|
|
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
|
|
|
|
|
System.out.println("---差集 reduce1 (list1 - list2)---");
|
|
|
|
|
reduce1.parallelStream().forEach(System.out::println);
|
|
|
|
|
// 差集 (list2 - list1)
|
|
|
|
|
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
|
|
|
|
|
System.out.println("---差集 reduce2 (list2 - list1)---");
|
|
|
|
|
reduce2.parallelStream().forEach(System.out::println);
|
|
|
|
|
// 并集
|
|
|
|
|
List<String> listAll = list1.parallelStream().collect(Collectors.toList());
|
|
|
|
|
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
|
|
|
|
|
listAll.addAll(listAll2);
|
|
|
|
|
System.out.println("---并集 listAll---");
|
|
|
|
|
listAll.parallelStream().forEachOrdered(System.out::println);
|
|
|
|
|
// 去重并集
|
|
|
|
|
List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
|
|
|
|
|
System.out.println("---得到去重并集 listAllDistinct---");
|
|
|
|
|
listAllDistinct.parallelStream().forEachOrdered(System.out::println);
|
|
|
|
|
System.out.println("---原来的List1---");
|
|
|
|
|
list1.parallelStream().forEachOrdered(System.out::println);
|
|
|
|
|
System.out.println("---原来的List2---");
|
|
|
|
|
list2.parallelStream().forEachOrdered(System.out::println);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class LogUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|