|
@@ -3,6 +3,7 @@ package com.izouma.nineth.web;
|
|
|
import com.izouma.nineth.domain.FileObject;
|
|
import com.izouma.nineth.domain.FileObject;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.service.storage.StorageService;
|
|
import com.izouma.nineth.service.storage.StorageService;
|
|
|
|
|
+import com.izouma.nineth.utils.ImageUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
import org.apache.commons.io.FilenameUtils;
|
|
@@ -47,47 +48,53 @@ public class FileUploadController {
|
|
|
@RequestParam(value = "width", required = false) Integer width,
|
|
@RequestParam(value = "width", required = false) Integer width,
|
|
|
@RequestParam(value = "height", required = false) Integer height) throws IOException {
|
|
@RequestParam(value = "height", required = false) Integer height) throws IOException {
|
|
|
|
|
|
|
|
- if (width != null && height == null) {
|
|
|
|
|
- height = Integer.MAX_VALUE;
|
|
|
|
|
- }
|
|
|
|
|
- if (height != null && width == null) {
|
|
|
|
|
- width = Integer.MAX_VALUE;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
|
|
String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("")
|
|
|
.toLowerCase().replace("jpeg", "jpg");
|
|
.toLowerCase().replace("jpeg", "jpg");
|
|
|
- if (Pattern.matches("(jpg|png)", ext)) {
|
|
|
|
|
- File tmpFile = null;
|
|
|
|
|
- try {
|
|
|
|
|
- tmpFile = File.createTempFile("upload", "." + ext);
|
|
|
|
|
- FileUtils.copyToFile(file.getInputStream(), tmpFile);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- throw new BusinessException("上传失败");
|
|
|
|
|
- }
|
|
|
|
|
- PngQuant pngQuant = new PngQuant();
|
|
|
|
|
- ImageIO.write(pngQuant.getRemapped(ImageIO.read(file.getInputStream())),
|
|
|
|
|
- "png", new File("/Users/drew/Downloads/111.png"));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
if (path == null) {
|
|
if (path == null) {
|
|
|
- String basePath = "application";
|
|
|
|
|
- try {
|
|
|
|
|
- basePath = file.getContentType().split("/")[0];
|
|
|
|
|
- } catch (Exception ignored) {
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ String basePath = Optional.ofNullable(file.getContentType()).orElse("application").split("/")[0];
|
|
|
path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
+ RandomStringUtils.randomAlphabetic(8)
|
|
+ RandomStringUtils.randomAlphabetic(8)
|
|
|
- + "." + FilenameUtils.getExtension(file.getOriginalFilename());
|
|
|
|
|
|
|
+ + "." + ext;
|
|
|
}
|
|
}
|
|
|
- InputStream is;
|
|
|
|
|
- try {
|
|
|
|
|
- is = file.getInputStream();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- log.error("上传失败", e);
|
|
|
|
|
- throw new BusinessException("上传失败", e.getMessage());
|
|
|
|
|
|
|
+
|
|
|
|
|
+ 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.resizePng(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.resizePng(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(is, path);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return storageService.uploadFromInputStream(file.getInputStream(), path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/base64")
|
|
@PostMapping("/base64")
|