|
|
@@ -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) {
|