diff --git a/04.系统编码/Backend/pom.xml b/04.系统编码/Backend/pom.xml index d2aa623..2e327c8 100644 --- a/04.系统编码/Backend/pom.xml +++ b/04.系统编码/Backend/pom.xml @@ -74,6 +74,14 @@ + + maven-compiler-plugin + org.apache.maven.plugins + + 15 + 15 + + diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/CorsConfig.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/CorsConfig.java index d785174..f040229 100644 --- a/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/CorsConfig.java +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/CorsConfig.java @@ -1,7 +1,9 @@ package com.userinformation.backend.config; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -14,4 +16,12 @@ public class CorsConfig implements WebMvcConfigurer { .allowedOrigins("*") .allowedMethods("POST", "GET", "OPTIONS"); } + + @Value("${custom.image.path}") + private String path; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/product/**").addResourceLocations("file:" + path + "/"); + } } \ No newline at end of file diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/WebMvcConfig.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/WebMvcConfig.java index 2debb5a..0a99c14 100644 --- a/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/WebMvcConfig.java +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/config/WebMvcConfig.java @@ -11,14 +11,14 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter * @author: xiaowuler * @createTime: 2021-12-03 09:33 */ -@Configuration -public class WebMvcConfig extends WebMvcConfigurerAdapter { - - @Value("${custom.image.path}") - private String path; - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/product/**").addResourceLocations("file:" + path + "/"); - } -} +//@Configuration +//public class WebMvcConfig extends WebMvcConfigurerAdapter { +// +// @Value("${custom.image.path}") +// private String path; +// +// @Override +// public void addResourceHandlers(ResourceHandlerRegistry registry) { +// registry.addResourceHandler("/product/**").addResourceLocations("file:" + path + "/"); +// } +//} diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/controller/ImageController.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/controller/ImageController.java index 19af178..890bdee 100644 --- a/04.系统编码/Backend/src/main/java/com/userinformation/backend/controller/ImageController.java +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/controller/ImageController.java @@ -4,8 +4,10 @@ import java.io.IOException; import java.util.List; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import com.userinformation.backend.model.vo.ImageVO; import com.userinformation.backend.service.ImageService; import com.userinformation.backend.util.RequestResult; @@ -29,7 +31,7 @@ public class ImageController { } @PostMapping("createGif") - public RequestResult createGif(List files) throws IOException { - return RequestResult.success(imageService.createGif(files)); + public RequestResult createGif(@RequestBody ImageVO image) throws IOException { + return RequestResult.success(imageService.createGif(image)); } } diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/handler/GlobalDefaultExceptionHandler.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/handler/GlobalDefaultExceptionHandler.java new file mode 100644 index 0000000..3cd93eb --- /dev/null +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/handler/GlobalDefaultExceptionHandler.java @@ -0,0 +1,18 @@ +package com.userinformation.backend.handler; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import com.userinformation.backend.util.RequestResult; + +@ControllerAdvice +@Slf4j +public class GlobalDefaultExceptionHandler { + @ExceptionHandler(Exception.class) + @ResponseBody + public RequestResult defaultExceptionHandler(Exception e) { + log.error("An error has occurred", e); + return RequestResult.fail(e.getMessage()); + } +} diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/model/vo/ImageVO.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/model/vo/ImageVO.java new file mode 100644 index 0000000..0a26515 --- /dev/null +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/model/vo/ImageVO.java @@ -0,0 +1,20 @@ +package com.userinformation.backend.model.vo; + +import java.util.List; + +import lombok.Data; + +/** + * @describe: 图片接收类 + * @author: xiaowuler + * @createTime: 2021-12-05 19:23 + */ +@Data +public class ImageVO { + private String model; + private String station; + private String elementCode; + private String type; + private String time; + private List urls; +} diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/service/ImageService.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/service/ImageService.java index 7b2bf0a..a71deb0 100644 --- a/04.系统编码/Backend/src/main/java/com/userinformation/backend/service/ImageService.java +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/service/ImageService.java @@ -4,13 +4,19 @@ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Objects; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import com.madgag.gif.fmsware.AnimatedGifEncoder; +import com.userinformation.backend.model.vo.ImageVO; +import com.userinformation.backend.util.FileUtil; /** * @describe: 图片处理 逻辑层 @@ -26,6 +32,9 @@ public class ImageService { @Value("${custom.image.url}") private String imageUrl; + @Value("${custom.image.gifPath}") + private String gifPath; + private final static String TIF_FILE_SUFFIX = ".tif"; private final static String PNG_FILE_SUFFIX = ".png"; @@ -43,31 +52,53 @@ public class ImageService { return targetFilePath.replace(imagePath, imageUrl); } - public String createGif(List files) { + public String createGif(ImageVO image) { + String targetPath = toTargetPath(image); + FileUtil.createFolder(targetPath); + if (Files.exists(Paths.get(targetPath))){ + return targetPath.replace(imagePath, imageUrl); + } + + List files = prepareImages(image.getUrls()); + Assert.isTrue(!files.isEmpty(), "没有有效数据,无法生成动图"); try { AnimatedGifEncoder e = new AnimatedGifEncoder(); e.setRepeat(0); - e.start("C:/Users/xiaowuler/Desktop/test1.gif"); - String pic[] = new String[] { - "d:\\Pictures\\1.jpg", - "d:\\Pictures\\2.jpg", - "d:\\Pictures\\6.jpg", - "d:\\Pictures\\8.jpg"}; -// BufferedImage src[] = new BufferedImage[pic.length]; - for(String filepath : files){ - e.setDelay(200); //设置播放的延迟时间 - BufferedImage bufferedImage = ImageIO.read(new File(filepath)); // 读入需要播放的jpg文件 - e.addFrame(bufferedImage); //添加到帧中 + e.start(targetPath); + for(File file : files){ + e.setDelay(500); + BufferedImage bufferedImage = ImageIO.read(file); + e.addFrame(bufferedImage); } -// for (int i = 0; i < src.length; i++) { -// e.setDelay(200); //设置播放的延迟时间 -// src[i] = ImageIO.read(new File(pic[i])); // 读入需要播放的jpg文件 -// e.addFrame(src[i]); //添加到帧中 -// } e.finish(); } catch (IOException e) { e.printStackTrace(); } - return null; + return targetPath.replace(imagePath, imageUrl); + } + + private List prepareImages(List urls){ + List files = new ArrayList<>(); + for(String url : urls){ + String filepath = url.replace(imageUrl, imagePath); + File file = new File(filepath); + if (file.exists()){ + files.add(file); + } + } + return files; + } + + private String toTargetPath(ImageVO image){ + String targetFilePath = "%s/%s/%s/".formatted(gifPath, image.getModel(), image.getStation()); + if (Objects.nonNull(image.getElementCode())){ + targetFilePath += image.getElementCode() + '/'; + } + + if (Objects.nonNull(image.getType())){ + targetFilePath += image.getType() + '/'; + } + + return "%s%s.gif".formatted(targetFilePath, image.getTime()); } } diff --git a/04.系统编码/Backend/src/main/java/com/userinformation/backend/util/FileUtil.java b/04.系统编码/Backend/src/main/java/com/userinformation/backend/util/FileUtil.java new file mode 100644 index 0000000..42a584b --- /dev/null +++ b/04.系统编码/Backend/src/main/java/com/userinformation/backend/util/FileUtil.java @@ -0,0 +1,19 @@ +package com.userinformation.backend.util; + +import java.io.File; + +/** + * @describe: 文件工具类 + * @author: xiaowuler + * @createTime: 2021-12-05 19:36 + */ +public class FileUtil { + public static void createFolder(String saveFilepath){ + File file = new File(saveFilepath); + File parentFile = file.getParentFile(); + if (parentFile.exists()){ + return; + } + parentFile.mkdirs(); + } +} diff --git a/04.系统编码/Backend/src/main/resources/application.yml b/04.系统编码/Backend/src/main/resources/application.yml index 6604633..73d76a2 100644 --- a/04.系统编码/Backend/src/main/resources/application.yml +++ b/04.系统编码/Backend/src/main/resources/application.yml @@ -2,17 +2,18 @@ server: port: 8002 spring: datasource: -# url: jdbc:mysql://${custom.parent-url}:33306/user_information?useUnicode=true&characteEncoding=utf-8 -# username: root -# password: 3cqscbr@only1 - url: jdbc:mysql://10.124.102.10:3306/user_information?useUnicode=true&characteEncoding=utf-8 + url: jdbc:mysql://112.124.40.88:33306/user_information?useUnicode=true&characteEncoding=utf-8 username: root - password: Njsqxj_sthj@2021 + password: 3cqscbr@only1 +# url: jdbc:mysql://10.124.102.10:3306/user_information?useUnicode=true&characteEncoding=utf-8 +# username: root +# password: Njsqxj_sthj@2021 driver-class-name: com.mysql.cj.jdbc.Driver custom: image: url: ${custom.parent-url}:8002/product -# path: D:/Deployments/LamanRadar/product - path: /home/project/NJEnvironmentPlatform/html/product -# parent-url: http://112.124.40.88 - parent-url: http://10.124.102.10 + path: D:/Deployments/LamanRadar/product + gifPath: ${custom.image.path}/gif + parent-url: http://112.124.40.88 +# path: /home/project/NJEnvironmentPlatform/html/product +# parent-url: http://10.124.102.10 diff --git a/04.系统编码/Backend/src/test/java/com/userinformation/backend/UserInformationApplicationTests.java b/04.系统编码/Backend/src/test/java/com/userinformation/backend/UserInformationApplicationTests.java index 0f00839..077638d 100644 --- a/04.系统编码/Backend/src/test/java/com/userinformation/backend/UserInformationApplicationTests.java +++ b/04.系统编码/Backend/src/test/java/com/userinformation/backend/UserInformationApplicationTests.java @@ -30,7 +30,7 @@ class UserInformationApplicationTests { add("C:\\Users\\xiaowuler\\Desktop\\images (2)\\TFLD_TX.png"); }}; - imageController.createGif(files); +// imageController.createGif(files); // File inputFile = new File("C:\\Users\\xiaowuler\\Desktop\\images\\decision-support\\plan-advice\\control-experiment\\TFLD_Q2.tif"); // File outputFile = new File("C:\\Users\\xiaowuler\\Desktop\\output.png"); // BufferedImage image = ImageIO.read(inputFile); diff --git a/04.系统编码/Frontend/.idea/workspace.xml b/04.系统编码/Frontend/.idea/workspace.xml index c15bfa6..f2b5d46 100644 --- a/04.系统编码/Frontend/.idea/workspace.xml +++ b/04.系统编码/Frontend/.idea/workspace.xml @@ -2,11 +2,19 @@ - - - + + + + + + + + + + + + -