From 0c569ab4b38d972213eeaf7e8965f33f8ec650fd Mon Sep 17 00:00:00 2001 From: luodangjia <luodangjia> Date: 星期二, 05 十一月 2024 11:19:19 +0800 Subject: [PATCH] 11.5 --- ruoyi-service/ruoyi-worker/src/main/java/com/ruoyi/worker/controller/OssController.java | 136 +++++++++++++++++++++++++++++++++++++-------- 1 files changed, 111 insertions(+), 25 deletions(-) diff --git a/ruoyi-service/ruoyi-worker/src/main/java/com/ruoyi/worker/controller/OssController.java b/ruoyi-service/ruoyi-worker/src/main/java/com/ruoyi/worker/controller/OssController.java index b3592ba..7ce8ac6 100644 --- a/ruoyi-service/ruoyi-worker/src/main/java/com/ruoyi/worker/controller/OssController.java +++ b/ruoyi-service/ruoyi-worker/src/main/java/com/ruoyi/worker/controller/OssController.java @@ -12,7 +12,9 @@ import io.swagger.annotations.ApiOperation; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -30,6 +32,7 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; +import java.util.concurrent.CompletableFuture; /** @@ -71,42 +74,125 @@ return R.ok(tencentCosUtil.upLoadFile(file)); } +// @PostMapping(value = "/pic", produces = "application/json;charset=UTF-8") +// @ApiOperation(value = "文件上传加水印", tags = "师傅端-文件上传") +// @ApiImplicitParams({ +// @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) +// }) +// public R<String> pic(@RequestParam("file") MultipartFile file,String longitude, +// String latitude) { +//// Result<String> address = GaoDeMapUtil.getAddress(longitude, latitude); +////// return R.fail(); +//// +//// // 获取当前时间 +//// +//// LocalDateTime date = LocalDateTime.now(); +//// +//// // 设置日期格式 +//// +//// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); +//// +//// // 通过format调用转换的方法 +//// +//// String dateWatermark = formatter.format(date); +////// System.err.println("==========="+LocalDateTime.now()); +//// log.error("==========="+LocalDateTime.now()); +//// +//// MultipartFile watermarkFile = FileImageWatermarkUtils.markWithContent(file,dateWatermark,address.getDatas()); +////// System.err.println("==========="+LocalDateTime.now()); +//// log.error("==========="+LocalDateTime.now()); +//// +//// +//// String url = tencentCosUtil.upLoadFile(watermarkFile); +////// String url = tencentCosUtil.upLoadFile(file); +//// log.error("==========="+LocalDateTime.now()); +//// return R.ok(url); +// +// +// // 参数校验 +// if (file == null || file.isEmpty()) { +// return R.fail("文件不能为空"); +// } +// if (longitude == null || longitude.isEmpty() || latitude == null || latitude.isEmpty()) { +// return R.fail("经纬度参数不能为空"); +// } +// +// try { +// // 获取地址信息 +// Result<String> addressResult = GaoDeMapUtil.getAddress(longitude, latitude); +// if (addressResult == null || addressResult.getDatas() == null) { +// return R.fail("无法获取地址信息"); +// } +// +// // 获取当前时间 +// LocalDateTime date = LocalDateTime.now(); +// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); +// String dateWatermark = formatter.format(date); +// log.info("===========" + dateWatermark); +// +// // 给图片加水印 +// MultipartFile watermarkFile = FileImageWatermarkUtils.markWithContent(file, dateWatermark, addressResult.getDatas()); +// log.info("水印添加成功"); +// +// // 上传到腾讯云COS +// String url = tencentCosUtil.upLoadFile(watermarkFile); +// log.info("文件上传成功,URL: " + url); +// +// return R.ok(url); +// } catch (Exception e) { +// log.error("文件上传加水印失败: ", e); +// return R.fail("文件上传加水印失败: " + e.getMessage()); +// } +// } + + @Autowired + private ThreadPoolTaskExecutor taskExecutor; @PostMapping(value = "/pic", produces = "application/json;charset=UTF-8") @ApiOperation(value = "文件上传加水印", tags = "师傅端-文件上传") @ApiImplicitParams({ @ApiImplicitParam(value = "文件", name = "file", dataType = "MultipartFile", required = true) }) - @Synchronized - public R<String> pic(@RequestParam("file") MultipartFile file,String longitude,String address, - String latitude) { -// Result<String> address = GaoDeMapUtil.getAddress(longitude, latitude); -// return R.fail(); + public CompletableFuture<R<String>> pic(@RequestParam("file") MultipartFile file, String longitude, String latitude) { + // 参数校验 + if (file == null || file.isEmpty()) { + return CompletableFuture.completedFuture(R.fail("文件不能为空")); + } + if (longitude == null || longitude.isEmpty() || latitude == null || latitude.isEmpty()) { + return CompletableFuture.completedFuture(R.fail("经纬度参数不能为空")); + } - // 获取当前时间 + return CompletableFuture.supplyAsync(() -> { + try { + // 获取地址信息 + Result<String> addressResult = GaoDeMapUtil.getAddress(longitude, latitude); + if (addressResult == null || addressResult.getDatas() == null) { + return R.fail("无法获取地址信息"); + } - LocalDateTime date = LocalDateTime.now(); + // 获取当前时间 + LocalDateTime date = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + String dateWatermark = formatter.format(date); + log.info("===========" + dateWatermark); - // 设置日期格式 + // 给图片加水印 + MultipartFile watermarkFile = FileImageWatermarkUtils.markWithContent(file, dateWatermark, addressResult.getDatas()); + log.info("水印添加成功"); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + // 上传到腾讯云COS + String url = tencentCosUtil.upLoadFile(watermarkFile); + log.info("文件上传成功,URL: " + url); - // 通过format调用转换的方法 - - String dateWatermark = formatter.format(date); -// System.err.println("==========="+LocalDateTime.now()); - log.error("==========="+LocalDateTime.now()); - - MultipartFile watermarkFile = FileImageWatermarkUtils.markWithContent(file,dateWatermark,address); -// System.err.println("==========="+LocalDateTime.now()); - log.error("==========="+LocalDateTime.now()); - - - String url = tencentCosUtil.upLoadFile(watermarkFile); -// String url = tencentCosUtil.upLoadFile(file); - log.error("==========="+LocalDateTime.now()); - return R.ok(url); - + return R.ok(url); + } catch (Exception e) { + log.error("文件上传加水印失败: ", e); + return R.fail("文件上传加水印失败: " + e.getMessage()); + } + }, taskExecutor); } + + + } -- Gitblit v1.7.1