|
|
@@ -57,7 +57,7 @@ public class FileUploadController {
|
|
|
+ "." + ext;
|
|
|
}
|
|
|
|
|
|
- if (Pattern.matches("(jpg|png)", ext) && compress) {
|
|
|
+ if (Pattern.matches("(jpg|png)", ext) && (compress || width != null || height != null)) {
|
|
|
if (width == null && height == null) {
|
|
|
width = Integer.MAX_VALUE;
|
|
|
height = Integer.MAX_VALUE;
|
|
|
@@ -68,88 +68,16 @@ public class FileUploadController {
|
|
|
}
|
|
|
BufferedImage img = null;
|
|
|
if ("jpg".equals(ext)) {
|
|
|
- img = ImageUtils.resizeJpg(file.getInputStream(), width, height);
|
|
|
+ img = ImageUtils.resizeJpg(file.getInputStream(), width, height, compress);
|
|
|
} 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);
|
|
|
+ img = ImageUtils.resizePng(file.getInputStream(), width, height, compress);
|
|
|
}
|
|
|
+ Objects.requireNonNull(img, "图片压缩失败");
|
|
|
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("/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);
|
|
|
}
|
|
|
|
|
|
@@ -198,7 +126,7 @@ public class FileUploadController {
|
|
|
String url;
|
|
|
|
|
|
|
|
|
- if (Pattern.matches("(jpg|png)", ext) && compress) {
|
|
|
+ if (Pattern.matches("(jpg|png)", ext) && (compress || width != null || height != null)) {
|
|
|
if (width == null && height == null) {
|
|
|
width = Integer.MAX_VALUE;
|
|
|
height = Integer.MAX_VALUE;
|
|
|
@@ -207,27 +135,11 @@ public class FileUploadController {
|
|
|
} else if (width == null) {
|
|
|
width = Integer.MAX_VALUE;
|
|
|
}
|
|
|
- BufferedImage img = null;
|
|
|
- if ("jpg".equals(ext)) {
|
|
|
- img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
|
|
|
- } else {
|
|
|
- img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, true);
|
|
|
- }
|
|
|
- ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(img, ext, os);
|
|
|
- InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
- url = storageService.uploadFromInputStream(is, path);
|
|
|
- } else if (Pattern.matches("(jpg|png)", ext) && (width != null || height != null)) {
|
|
|
- if (height == null) {
|
|
|
- height = Integer.MAX_VALUE;
|
|
|
- } else if (width == null) {
|
|
|
- width = Integer.MAX_VALUE;
|
|
|
- }
|
|
|
- BufferedImage img = null;
|
|
|
+ BufferedImage img;
|
|
|
if ("jpg".equals(ext)) {
|
|
|
- img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height);
|
|
|
+ img = ImageUtils.resizeJpg(new FileInputStream(tmpFile), width, height, compress);
|
|
|
} else {
|
|
|
- img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, false);
|
|
|
+ img = ImageUtils.resizePng(new FileInputStream(tmpFile), width, height, compress);
|
|
|
}
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
ImageIO.write(img, ext, os);
|
|
|
@@ -241,26 +153,26 @@ public class FileUploadController {
|
|
|
|
|
|
|
|
|
if ("mp4".equalsIgnoreCase(ext)) {
|
|
|
- FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
|
|
|
- frameGrabber.start();
|
|
|
- Java2DFrameConverter aa = new Java2DFrameConverter();
|
|
|
+ String thumbPath = "thumb_image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
+ + RandomStringUtils.randomAlphabetic(8) + ".jpg";
|
|
|
|
|
|
+ FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
|
|
|
+ Java2DFrameConverter frameConverter = new Java2DFrameConverter();
|
|
|
try {
|
|
|
- BufferedImage bi;
|
|
|
- Frame f = frameGrabber.grabKeyFrame();
|
|
|
+ frameGrabber.start();
|
|
|
|
|
|
- bi = aa.convert(f);
|
|
|
- File thumbFile = null;
|
|
|
- while (bi != null) {
|
|
|
- thumbFile = File.createTempFile("video_thumb_", ".jpg");
|
|
|
- ImageIO.write(bi, "jpg", thumbFile);
|
|
|
- f = frameGrabber.grabKeyFrame();
|
|
|
- bi = aa.convert(f);
|
|
|
+ Frame frame = null;
|
|
|
+ while (frame == null) {
|
|
|
+ frame = frameGrabber.grabKeyFrame();
|
|
|
}
|
|
|
- Objects.requireNonNull(thumbFile);
|
|
|
- String thumbPath = "thumb_image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
- + RandomStringUtils.randomAlphabetic(8) + ".jpg";
|
|
|
- thumbUrl = storageService.uploadFromInputStream(new FileInputStream(thumbFile), thumbPath);
|
|
|
+ Objects.requireNonNull(frame, "获取视频缩略图失败");
|
|
|
+ BufferedImage thumbBi = frameConverter.convert(frame);
|
|
|
+
|
|
|
+ BufferedImage thumbResized = ImageUtils.resizeJpg(thumbBi, 1000, 1000, false);
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(thumbResized, "jpg", os);
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ thumbUrl = storageService.uploadFromInputStream(is, thumbPath);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|