xiongzhu 4 years ago
parent
commit
53ed4fa5ff

+ 12 - 1
src/main/java/com/izouma/nineth/utils/ImageUtils.java

@@ -30,8 +30,19 @@ public class ImageUtils {
         return img;
     }
 
+    public static BufferedImage resizePng1(InputStream image, int width, int height, boolean compress) throws IOException {
+        BufferedImage bufferedImage = ImageIO.read(image);
+        BufferedImage img = Thumbnails.of(bufferedImage)
+                .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
+                .outputFormat("png")
+                .asBufferedImage();
+        bufferedImage.flush();
+        PngQuant pngQuant = new PngQuant();
+        return pngQuant.getRemapped(img);
+    }
+
     public static void main(String[] args) throws IOException {
-        ImageUtils.resizePng(new FileInputStream("/Users/drew/Downloads/盲盒详情.png"), 1000, 2000,true);
+        ImageUtils.resizePng(new FileInputStream("/Users/drew/Downloads/盲盒详情.png"), 1000, 2000, true);
         PngQuant pngQuant = new PngQuant();
         ImageIO.write(pngQuant
                         .getRemapped(ImageIO.read(new File("/Users/drew/Desktop/1.png"))),

+ 56 - 0
src/main/java/com/izouma/nineth/web/FileUploadController.java

@@ -97,6 +97,62 @@ public class FileUploadController {
         return storageService.uploadFromInputStream(file.getInputStream(), path);
     }
 
+    @PostMapping("/file1")
+    public String uploadFile1(@RequestParam("file") MultipartFile file,
+                             @RequestParam(value = "path", required = false) String path,
+                             @RequestParam(value = "compress", defaultValue = "false") boolean compress,
+                             @RequestParam(value = "width", required = false) Integer width,
+                             @RequestParam(value = "height", required = false) Integer height) throws IOException {
+
+        String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
+                .toLowerCase().replace("jpeg", "jpg");
+        if (path == null) {
+            String basePath = Optional.ofNullable(file.getContentType()).orElse("application").split("/")[0];
+            path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+                    + RandomStringUtils.randomAlphabetic(8)
+                    + "." + ext;
+        }
+
+        if (Pattern.matches("(jpg|png)", ext) && compress) {
+            if (width == null && height == null) {
+                width = Integer.MAX_VALUE;
+                height = Integer.MAX_VALUE;
+            } else if (height == null) {
+                height = Integer.MAX_VALUE;
+            } else if (width == null) {
+                width = Integer.MAX_VALUE;
+            }
+            BufferedImage img = null;
+            if ("jpg".equals(ext)) {
+                img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
+            } else if ("png".equals(ext)) {
+                img = ImageUtils.resizePng1(file.getInputStream(), width, height, true);
+            }
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            ImageIO.write(img, ext, os);
+            InputStream is = new ByteArrayInputStream(os.toByteArray());
+            return storageService.uploadFromInputStream(is, path);
+        } else if (width != null || height != null) {
+            if (height == null) {
+                height = Integer.MAX_VALUE;
+            } else if (width == null) {
+                width = Integer.MAX_VALUE;
+            }
+            BufferedImage img = null;
+            if ("jpg".equals(ext)) {
+                img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
+            } else if ("png".equals(ext)) {
+                img = ImageUtils.resizePng1(file.getInputStream(), width, height, false);
+            }
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            ImageIO.write(img, ext, os);
+            InputStream is = new ByteArrayInputStream(os.toByteArray());
+            return storageService.uploadFromInputStream(is, path);
+        }
+
+        return storageService.uploadFromInputStream(file.getInputStream(), path);
+    }
+
     @PostMapping("/base64")
     public String uploadImage(@RequestParam("base64") String base64,
                               @RequestParam(value = "path", required = false) String path) {