FileController.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.webchat.sso.controller;
  2. import com.webchat.common.bean.APIResponseBean;
  3. import com.webchat.common.bean.APIResponseBeanUtil;
  4. import com.webchat.common.service.MinioService;
  5. import com.webchat.common.util.DateUtils;
  6. import com.webchat.domain.vo.response.UploadResultVO;
  7. import jakarta.servlet.http.HttpServletRequest;
  8. import jakarta.servlet.http.HttpServletResponse;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.util.Assert;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import java.util.Date;
  19. @RestController
  20. @RequestMapping("/sso-service/file")
  21. public class FileController {
  22. @Autowired
  23. private MinioService minioService;
  24. @Autowired
  25. private HttpServletRequest request;
  26. @PostMapping("/upload")
  27. public APIResponseBean<UploadResultVO> upload(MultipartFile file) {
  28. String path = request.getHeader("upload-path");
  29. Assert.isTrue(StringUtils.isNotBlank(path), "上传路径不能为空");
  30. String timePackage = DateUtils.getDate2String(DateUtils.YYYYMMDD, new Date());
  31. UploadResultVO uploadResultDTO = minioService.upload(path + "/" + timePackage, file, true);
  32. return APIResponseBeanUtil.success(uploadResultDTO);
  33. }
  34. @GetMapping("/download")
  35. public void download(@RequestParam String filename, HttpServletResponse response) {
  36. minioService.download(filename, response);
  37. }
  38. }