SysFileController.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.boman.file.controller;
  2. import com.boman.domain.dto.AjaxResult;
  3. import com.boman.domain.dto.Base64Dto;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestBody;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import com.boman.domain.dto.R;
  13. import com.boman.common.core.utils.file.FileUtils;
  14. import com.boman.file.service.ISysFileService;
  15. import com.boman.domain.SysFile;
  16. /**
  17. * 文件请求处理
  18. *
  19. * @author ruoyi
  20. */
  21. @RestController
  22. public class SysFileController
  23. {
  24. private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
  25. @Autowired
  26. private ISysFileService sysFileService;
  27. /**
  28. * 文件上传请求
  29. */
  30. @PostMapping("upload")
  31. public R<SysFile> upload(MultipartFile file)
  32. {
  33. try
  34. {
  35. // 上传并返回访问地址
  36. String url = sysFileService.uploadFile(file);
  37. SysFile sysFile = new SysFile();
  38. sysFile.setName(FileUtils.getName(url));
  39. sysFile.setUrl(url);
  40. return R.ok(sysFile);
  41. }
  42. catch (Exception e)
  43. {
  44. log.error("上传文件失败", e);
  45. return R.fail(e.getMessage());
  46. }
  47. }
  48. /**
  49. * 通用上传请求 暂且如此处理,后来再优化
  50. */
  51. @PostMapping("/upload/base64")
  52. public AjaxResult uploadFileBase64(@RequestBody Base64Dto dto) {
  53. try {
  54. String base64 = dto.getFile();
  55. String url = sysFileService.uploadFileBase64(base64);
  56. AjaxResult ajax = AjaxResult.success();
  57. ajax.put("name", FileUtils.getName(url));
  58. ajax.put("url", url);
  59. return ajax;
  60. } catch (Exception e) {
  61. return AjaxResult.error(e.getMessage());
  62. }
  63. }
  64. @GetMapping("/getConfigPath")
  65. public String getConfigPath(){
  66. return sysFileService.getConfigPath();
  67. }
  68. }