FileUploadController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.FileObject;
  3. import com.izouma.nineth.exception.BusinessException;
  4. import com.izouma.nineth.service.storage.StorageService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.io.FileUtils;
  7. import org.apache.commons.io.FilenameUtils;
  8. import org.apache.commons.lang3.ArrayUtils;
  9. import org.apache.commons.lang3.RandomStringUtils;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.bytedeco.javacv.FFmpegFrameGrabber;
  12. import org.bytedeco.javacv.Frame;
  13. import org.bytedeco.javacv.Java2DFrameConverter;
  14. import org.pngquant.PngQuant;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import javax.imageio.ImageIO;
  22. import java.awt.image.BufferedImage;
  23. import java.io.*;
  24. import java.net.URLConnection;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Base64;
  27. import java.util.Date;
  28. import java.util.Objects;
  29. import java.util.Optional;
  30. @RestController
  31. @RequestMapping("/upload")
  32. @Slf4j
  33. public class FileUploadController {
  34. @Autowired
  35. private StorageService storageService;
  36. @PostMapping("/file")
  37. public String uploadFile(@RequestParam("file") MultipartFile file,
  38. @RequestParam(value = "path", required = false) String path) {
  39. if (path == null) {
  40. String basePath = "application";
  41. try {
  42. basePath = file.getContentType().split("/")[0];
  43. } catch (Exception ignored) {
  44. }
  45. path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  46. + RandomStringUtils.randomAlphabetic(8)
  47. + "." + FilenameUtils.getExtension(file.getOriginalFilename());
  48. }
  49. InputStream is;
  50. try {
  51. is = file.getInputStream();
  52. } catch (IOException e) {
  53. log.error("上传失败", e);
  54. throw new BusinessException("上传失败", e.getMessage());
  55. }
  56. return storageService.uploadFromInputStream(is, path);
  57. }
  58. @PostMapping("/base64")
  59. public String uploadImage(@RequestParam("base64") String base64,
  60. @RequestParam(value = "path", required = false) String path) {
  61. base64 = base64.substring(base64.indexOf(',') + 1);
  62. Base64.Decoder decoder = Base64.getDecoder();
  63. if (path == null) {
  64. String ext = ".jpg";
  65. try {
  66. InputStream is = new ByteArrayInputStream(decoder.decode(base64.getBytes()));
  67. String type = URLConnection.guessContentTypeFromStream(is);
  68. ext = type.replace("image/", ".").replace("jpeg", "jpg");
  69. } catch (Exception ignored) {
  70. }
  71. path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  72. + RandomStringUtils.randomAlphabetic(8) + ext;
  73. }
  74. InputStream is;
  75. try {
  76. is = new ByteArrayInputStream(decoder.decode(base64.getBytes()));
  77. } catch (Exception e) {
  78. log.error("上传失败", e);
  79. throw new BusinessException("上传失败", e.getMessage());
  80. }
  81. return storageService.uploadFromInputStream(is, path);
  82. }
  83. @PostMapping("/fileObject")
  84. public FileObject uploadFileObject(@RequestParam("file") MultipartFile file) throws IOException {
  85. String ext = Optional.ofNullable(FilenameUtils.getExtension(file.getOriginalFilename())).orElse("");
  86. String[] extList = new String[]{"jpg", "jpeg", "png", "gif", "mp4"};
  87. if (!ArrayUtils.contains(extList, ext.toLowerCase())) {
  88. throw new BusinessException("仅支持" + StringUtils.join(extList, "、") + "格式");
  89. }
  90. File tmpFile = File.createTempFile("upload_", "." + ext);
  91. FileUtils.copyToFile(file.getInputStream(), tmpFile);
  92. String path = "nft/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  93. + RandomStringUtils.randomAlphabetic(8)
  94. + "." + ext;
  95. String url = storageService.uploadFromInputStream(new FileInputStream(tmpFile), path);
  96. String thumbUrl = null;
  97. if ("mp4".equalsIgnoreCase(ext)) {
  98. FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(tmpFile);
  99. frameGrabber.start();
  100. Java2DFrameConverter aa = new Java2DFrameConverter();
  101. try {
  102. BufferedImage bi;
  103. Frame f = frameGrabber.grabKeyFrame();
  104. bi = aa.convert(f);
  105. File thumbFile = null;
  106. while (bi != null) {
  107. thumbFile = File.createTempFile("video_thumb_", ".png");
  108. PngQuant pngQuant = new PngQuant();
  109. ImageIO.write(pngQuant.getRemapped(bi), "png", thumbFile);
  110. f = frameGrabber.grabKeyFrame();
  111. bi = aa.convert(f);
  112. }
  113. Objects.requireNonNull(thumbFile);
  114. String thumbPath = "thumb_image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  115. + RandomStringUtils.randomAlphabetic(8) + ".png";
  116. thumbUrl = storageService.uploadFromInputStream(new FileInputStream(thumbFile), thumbPath);
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. } finally {
  120. frameGrabber.stop();
  121. }
  122. }
  123. return new FileObject(file.getOriginalFilename(), url, thumbUrl, file.getContentType());
  124. }
  125. }