FileUploadController.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.izouma.mr.web;
  2. import com.izouma.mr.exception.BusinessException;
  3. import com.izouma.mr.service.storage.StorageService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.io.FilenameUtils;
  6. import org.apache.commons.lang3.RandomStringUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.net.URLConnection;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Base64;
  19. import java.util.Date;
  20. @RestController
  21. @RequestMapping("/upload")
  22. @Slf4j
  23. public class FileUploadController {
  24. @Autowired
  25. private StorageService storageService;
  26. @PostMapping("/file")
  27. public String uploadFile(@RequestParam("file") MultipartFile file,
  28. @RequestParam(value = " ", required = false) String path) {
  29. if (path == null) {
  30. String basePath = "application";
  31. try {
  32. basePath = file.getContentType().split("/")[0];
  33. } catch (Exception ignored) {
  34. }
  35. path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  36. + RandomStringUtils.randomAlphabetic(8)
  37. + "." + FilenameUtils.getExtension(file.getOriginalFilename());
  38. }
  39. InputStream is;
  40. try {
  41. is = file.getInputStream();
  42. } catch (IOException e) {
  43. log.error("上传失败", e);
  44. throw new BusinessException("上传失败", e.getMessage());
  45. }
  46. return storageService.uploadFromInputStream(is, path);
  47. }
  48. @PostMapping("/base64")
  49. public String uploadImage(@RequestParam("base64") String base64,
  50. @RequestParam(value = "path", required = false) String path) {
  51. base64 = base64.substring(base64.indexOf(',') + 1);
  52. Base64.Decoder decoder = Base64.getDecoder();
  53. if (path == null) {
  54. String ext = ".jpg";
  55. try {
  56. InputStream is = new ByteArrayInputStream(decoder.decode(base64.getBytes()));
  57. String type = URLConnection.guessContentTypeFromStream(is);
  58. ext = type.replace("image/", ".").replace("jpeg", "jpg");
  59. } catch (Exception ignored) {
  60. }
  61. path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  62. + RandomStringUtils.randomAlphabetic(8) + ext;
  63. }
  64. InputStream is;
  65. try {
  66. is = new ByteArrayInputStream(decoder.decode(base64.getBytes()));
  67. } catch (Exception e) {
  68. log.error("上传失败", e);
  69. throw new BusinessException("上传失败", e.getMessage());
  70. }
  71. return storageService.uploadFromInputStream(is, path);
  72. }
  73. }