FileUploadController.java 3.0 KB

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