diff --git a/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java b/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java index eea7d50..8cbaa2c 100644 --- a/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java +++ b/docus-api-common/src/main/java/com/docus/server/common/service/impl/FileUploadServiceImpl.java @@ -1,5 +1,7 @@ package com.docus.server.common.service.impl; +import cn.hutool.core.img.ImgUtil; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.docus.core.util.Func; import com.docus.infrastructure.web.exception.ApiException; @@ -11,8 +13,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.awt.*; +import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -28,9 +33,12 @@ import java.util.UUID; @Slf4j public class FileUploadServiceImpl implements IFileUploadService { - @Value("${file.uploadFolder:D://docus/}") + @Value("${file.uploadFolder:D://docus/source/}") private String saveFilePath; + @Value("${file.uploadCutFolder:#{null}}") + private String saveCutFilePath; + private static final String UPLOAD_FOLDER = "upload"; private static DateTimeFormatter ymdDtf = DateTimeFormatter.ofPattern("yyyyMMdd"); @@ -58,18 +66,16 @@ public class FileUploadServiceImpl implements IFileUploadService { String todayFormat = ymdDtf.format(LocalDateTime.now()); - String path = pathKey + "/" + todayFormat + "/" + UUID.randomUUID().toString() + "/"; + String path = pathKey + "/" + todayFormat + "/" + UUID.randomUUID().toString().replace("-", "") + "/"; File fileDir = new File(saveFilePath + path); + if (!fileDir.exists()) { fileDir.mkdirs(); } - String fileName = path + multipartFile.getOriginalFilename(); + String fileName = path + multipartFile.getOriginalFilename(); File dest = new File(saveFilePath + fileName); - multipartFile.transferTo(dest); - - UploadFileVO uploadFileVO = new UploadFileVO(); uploadFileVO.setFileName(multipartFile.getOriginalFilename()); uploadFileVO.setFileSize(multipartFile.getSize()); @@ -77,6 +83,28 @@ public class FileUploadServiceImpl implements IFileUploadService { uploadFileVO.setFileType(multipartFile.getContentType()); uploadFileVO.setFilePath(fileName); + if (Func.isNotBlank(saveCutFilePath)) { + File cutFileDir = new File(saveCutFilePath + path); + if (!cutFileDir.exists()) { + cutFileDir.mkdirs(); + } + File cutDest = new File(saveCutFilePath + fileName); + + BufferedImage image = ImageIO.read(multipartFile.getInputStream()); + int height = image.getHeight(); + int width = image.getWidth(); + int minX = image.getMinX(); + int minY = image.getMinY(); + + ImgUtil.cut(image, FileUtil.file(cutDest), new Rectangle(50, 50, width, (height / 2) / 2)); + + uploadFileVO.setWidth(width); + uploadFileVO.setHeight(height); + uploadFileVO.setX(minX); + uploadFileVO.setY(minY); + } + + multipartFile.transferTo(dest); return uploadFileVO; } diff --git a/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java b/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java index 3d23125..ca2cde1 100644 --- a/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java +++ b/docus-client-interface/src/main/java/com/docus/server/vo/scheduling.management/schcollectorversionfile/UploadFileVO.java @@ -29,4 +29,19 @@ public class UploadFileVO { @TableField("file_type") private String fileType; + @ApiModelProperty(value = "高度") + @TableField("height") + private int height; + + @ApiModelProperty(value = "宽度") + @TableField("width") + private int width; + + @ApiModelProperty(value = "横坐标") + @TableField("x") + private int x; + + @ApiModelProperty(value = "纵坐标") + @TableField("y") + private int y; } diff --git a/docus-segmentation/src/main/java/com/docus/server/SegmentationBootstrap.java b/docus-segmentation/src/main/java/com/docus/server/SegmentationBootstrap.java index 04595f1..607f354 100644 --- a/docus-segmentation/src/main/java/com/docus/server/SegmentationBootstrap.java +++ b/docus-segmentation/src/main/java/com/docus/server/SegmentationBootstrap.java @@ -6,7 +6,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync -@EnableFeignClients(basePackages = {"com.docus.core.excel.feign", "com.docus.server.api.segmentation"}) +@EnableFeignClients(basePackages = {"com.docus.core.excel.feign", "com.docus.server.api"}) @SpringBootApplication(scanBasePackages = {"com.docus"}) public class SegmentationBootstrap { public static void main(String[] args) { diff --git a/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java b/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java index f8a8318..5b1ce61 100644 --- a/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java +++ b/docus-segmentation/src/main/java/com/docus/server/controller/FileController.java @@ -8,6 +8,7 @@ import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -38,7 +39,7 @@ public class FileController { @ApiImplicitParams({ @ApiImplicitParam(name = "files", value = "文件", required = true, dataTypeClass = MultipartFile.class) }) - public void uploadFile(MultipartFile[] files, String pathKey) throws Exception { + public void uploadFile(@RequestPart("files") MultipartFile[] files, String pathKey) throws Exception { iFileUploadService.uploadFile(files, pathKey); } } diff --git a/docus-segmentation/src/main/resources/bootstrap.yml b/docus-segmentation/src/main/resources/bootstrap.yml index 6f91123..a4665d3 100644 --- a/docus-segmentation/src/main/resources/bootstrap.yml +++ b/docus-segmentation/src/main/resources/bootstrap.yml @@ -51,7 +51,8 @@ mybatis-plus: type-enums-package: com.docus.server.enums file: - uploadFolder: D://docus/ + uploadFolder: D://docus/source/ + uploadCutFolder: D://docus/cut/ docus: vm-task-cron: 0/30 * * * * ? diff --git a/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java b/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java new file mode 100644 index 0000000..ae54829 --- /dev/null +++ b/docus-segmentation/src/test/java/com/docus/server/ImageProcessingExample.java @@ -0,0 +1,24 @@ +package com.docus.server; + +import cn.hutool.core.img.ImgUtil; +import cn.hutool.core.io.FileUtil; + +import javax.imageio.ImageIO; +import java.awt.*; + +public class ImageProcessingExample { + + public static void main(String[] args) throws Exception { + // 1. 调整图片大小 +// ImgUtil.scale(FileUtil.file("input.jpg"), FileUtil.file("output.jpg"), 0.5f); // 将input.jpg缩小为原来的一半,并保存为output.jpg + + // 2. 裁剪图片 +// ImgUtil.cut(FileUtil.file("C:\\Users\\dataexa\\Desktop\\麻醉.jpg"), FileUtil.file("C:\\Users\\dataexa\\Desktop\\output1.jpg"), new Rectangle(50, 50, 2400, (3527 / 2) / 2)); // 从input.jpg中裁剪出一个200x200的区域,保存为output.jpg + + // 5. 图片旋转 + Image image = ImgUtil.rotate(ImageIO.read(FileUtil.file("C:\\Users\\dataexa\\Desktop\\麻醉.jpg")), 90); + ImgUtil.write(image, FileUtil.file("C:\\Users\\dataexa\\Desktop\\output2.jpg")); + + } + +} diff --git a/pom.xml b/pom.xml index 72b3c7f..b7a4580 100644 --- a/pom.xml +++ b/pom.xml @@ -256,6 +256,13 @@ ant 1.10.5 + + + com.twelvemonkeys.imageio + imageio-tiff + 3.4.1 + +