FileController.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package com.zhentao.information.controller;
  2. import com.zhentao.osspicture.OssUtil;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. @Slf4j
  12. @RestController
  13. public class FileController {
  14. @Autowired
  15. private OssUtil ossUtil;
  16. @PostMapping("/api/file/upload")
  17. public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) {
  18. Map<String, Object> result = new HashMap<>();
  19. try {
  20. String fileUrl = ossUtil.uploadFile(file);
  21. result.put("url", fileUrl);
  22. result.put("fileName", file.getOriginalFilename());
  23. result.put("fileType", file.getContentType());
  24. result.put("fileSize", file.getSize());
  25. result.put("success", true);
  26. log.info("文件上传成功: {}", fileUrl);
  27. } catch (Exception e) {
  28. log.error("文件上传失败", e);
  29. result.put("success", false);
  30. result.put("message", "上传失败: " + e.getMessage());
  31. }
  32. return result;
  33. }
  34. }