From b55931a438449f8959b676494607991eb6ecfefa Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Sat, 14 Dec 2024 10:38:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 93 ++++++++++--------- .../coal/file/controller/FileController.java | 5 +- .../coal/file/service/FileService.java | 35 +++++-- .../controller/GoodsSearchController.java | 3 +- .../service/GoodsSearchService.java | 6 +- 5 files changed, 86 insertions(+), 56 deletions(-) diff --git a/pom.xml b/pom.xml index f1fb7357..b4c99194 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,13 @@ 5.8.31 + + + cn.hutool + hutool-http + 5.8.31 + + com.aliyun ocr_api20210707 @@ -404,13 +411,13 @@ commons-io 2.11.0 - + - - - + + + - + com.google.googlejavaformat google-java-format @@ -444,14 +451,14 @@ - - - - - - - - + + + + + + + + @@ -510,36 +517,36 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/cn/lihongjie/coal/file/controller/FileController.java b/src/main/java/cn/lihongjie/coal/file/controller/FileController.java index 5bdc85f0..2e45bca1 100644 --- a/src/main/java/cn/lihongjie/coal/file/controller/FileController.java +++ b/src/main/java/cn/lihongjie/coal/file/controller/FileController.java @@ -31,14 +31,15 @@ public class FileController { @PostMapping("/upload") public FileDto upload( - @RequestParam("file") MultipartFile file, + @RequestParam(value = "file", required = false) MultipartFile file, @RequestParam("name") String name, @RequestParam("dir") String dir, + @RequestParam(value = "url", required = false) String url, @RequestParam(value = "sortKey", required = false) String sortKey, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "remarks", required = false) String remarks ) { - return this.service.upload(file, name, dir, sortKey, code, remarks); + return this.service.upload(file, name, dir, sortKey, code, remarks, url); } @PostMapping("/update") diff --git a/src/main/java/cn/lihongjie/coal/file/service/FileService.java b/src/main/java/cn/lihongjie/coal/file/service/FileService.java index d8b84a58..4444f82e 100644 --- a/src/main/java/cn/lihongjie/coal/file/service/FileService.java +++ b/src/main/java/cn/lihongjie/coal/file/service/FileService.java @@ -1,5 +1,6 @@ package cn.lihongjie.coal.file.service; +import cn.hutool.http.HttpUtil; import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; import cn.lihongjie.coal.base.service.BaseService; @@ -34,6 +35,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.UUID; @@ -106,10 +109,27 @@ public class FileService extends BaseService { String dir, String sortKey, String code, - String remarks) { + String remarks, + String url) { + + long size = 0; + InputStream fis = null; + if (file != null) { + + size = file.getSize(); + fis = file.getInputStream(); + } else if (StringUtils.isNotEmpty(url)) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + HttpUtil.download(url, out, true); + fis = new ByteArrayInputStream(out.toByteArray()); + size = out.size(); + } else { + throw new BizException("文件为空"); + } + FileEntity fileEntity = new FileEntity(); fileEntity.setFileName(name); - fileEntity.setFileSize(file.getSize()); + fileEntity.setFileSize(size); fileEntity.setCode(code); fileEntity.setRemarks(remarks); try { @@ -125,7 +145,7 @@ public class FileService extends BaseService { activateProfile, Ctx.currentUser().getOrganizationId(), StringUtils.defaultIfBlank(dir, "public"))); - InputStream inputStream = file.getInputStream(); + InputStream inputStream = fis; fileEntity.setObjectId( UUID.randomUUID() + "." + FilenameUtils.getExtension(name.replace(":", "_"))); try (inputStream) { @@ -134,7 +154,7 @@ public class FileService extends BaseService { String objectKey = getObjectKey(fileEntity); - fileEntity.setPublicUrl(uploadToOSS(objectKey, file.getInputStream())); + fileEntity.setPublicUrl(uploadToOSS(objectKey, fis)); this.save(fileEntity); @@ -147,9 +167,7 @@ public class FileService extends BaseService { com.aliyun.oss.model.PutObjectResult objectResult = ossClient.putObject( - aliyunProperty.getOSS().getBucketName(), - objectKey, - inputStream); + aliyunProperty.getOSS().getBucketName(), objectKey, inputStream); } catch (Exception e) { log.warn("上传文件失败", e); @@ -224,8 +242,7 @@ public class FileService extends BaseService { try { OSSObject object = ossClient.getObject(aliyunProperty.getOSS().getBucketName(), objectKey); - return - object.getObjectContent(); + return object.getObjectContent(); } catch (Exception e) { log.warn("下载文件失败", e); throw new BizException("下载文件失败 %s".formatted(e.getMessage())); diff --git a/src/main/java/cn/lihongjie/coal/goodsSearch/controller/GoodsSearchController.java b/src/main/java/cn/lihongjie/coal/goodsSearch/controller/GoodsSearchController.java index af6c85e5..4e17206f 100644 --- a/src/main/java/cn/lihongjie/coal/goodsSearch/controller/GoodsSearchController.java +++ b/src/main/java/cn/lihongjie/coal/goodsSearch/controller/GoodsSearchController.java @@ -4,6 +4,7 @@ import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchRequest; import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchResponse; import cn.lihongjie.coal.goodsSearch.service.GoodsSearchService; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/goodsSearch") @@ -18,7 +19,7 @@ public class GoodsSearchController { } @RequestMapping("/search") - public GoodsSearchResponse search(GoodsSearchRequest request) { + public GoodsSearchResponse search(@RequestBody GoodsSearchRequest request) { return diff --git a/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java b/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java index f2a305c2..1deddd3c 100644 --- a/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java +++ b/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java @@ -21,8 +21,11 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; +import java.nio.charset.StandardCharsets; + @Service @Slf4j public class GoodsSearchService { @@ -44,11 +47,12 @@ public class GoodsSearchService { } @SneakyThrows + @Cacheable(cacheNames = "goodsSearch", key = "#request.code", condition = "#request.code != null", unless = "#result.flag.equals('true')") public GoodsSearchResponse search(GoodsSearchRequest request) { ApiResponse response = Client.getInstance().search(request.getCode()); - String bodyStr = response.getBodyStr(); + String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8); ObjectNode jsonNodes = objectMapper.readValue(bodyStr, ObjectNode.class);