FileController.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.cdu.lys.graduation.controller.file;
  2. import com.cdu.lys.graduation.commons.result.ActionResult;
  3. import com.cdu.lys.graduation.commons.result.UploadFileResponse;
  4. import com.cdu.lys.graduation.file.FileService;
  5. import com.cdu.lys.graduation.types.SystemConstant;
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.core.io.Resource;
  11. import org.springframework.http.HttpHeaders;
  12. import org.springframework.http.MediaType;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.validation.annotation.Validated;
  15. import org.springframework.web.bind.annotation.*;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
  18. import org.springframework.web.util.UriComponentsBuilder;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.validation.constraints.NotNull;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. /**
  26. * @author liyinsong
  27. * @date 2022/3/28 21:59
  28. */
  29. @Api(tags = "文件接口")
  30. @RestController
  31. @RequestMapping("/eorder/file")
  32. @Slf4j
  33. @Validated
  34. public class FileController {
  35. @Autowired
  36. private FileService fileService;
  37. @ApiOperation("图片上传")
  38. @PostMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_VALUE)
  39. public ActionResult<UploadFileResponse> uploadFile(@NotNull(message = "文件不能为空") MultipartFile file) {
  40. String fileName = fileService.storeFile(file);
  41. // String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
  42. // .path(SystemConstant.SYSTEM_DOWNLOAD_PATH)
  43. // .path(fileName)
  44. // .toUriString();
  45. String fileDownloadUri = SystemConstant.SYSTEM_DOWNLOAD_PATH + fileName;
  46. UploadFileResponse response = new UploadFileResponse(fileName, fileDownloadUri,
  47. file.getContentType(), file.getSize());
  48. return ActionResult.getSuccessResult(response);
  49. }
  50. // @PostMapping("/uploadMultipleFiles")
  51. public ActionResult<List<UploadFileResponse>> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
  52. List<UploadFileResponse> responses = new ArrayList<>();
  53. UriComponentsBuilder path = ServletUriComponentsBuilder.fromCurrentContextPath()
  54. .path(SystemConstant.SYSTEM_DOWNLOAD_PATH);
  55. Arrays.stream(files).forEach(file->{
  56. String filename = fileService.storeFile(file);
  57. UploadFileResponse response = new UploadFileResponse(filename, path.path(filename).toUriString(),
  58. file.getContentType(), file.getSize());
  59. responses.add(response);
  60. });
  61. return ActionResult.getSuccessResult(responses);
  62. }
  63. @ApiOperation("文件下载")
  64. @GetMapping("/download/{fileName:.+}")
  65. public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
  66. // Load file as Resource
  67. Resource resource = fileService.loadFileAsResource(fileName);
  68. // Try to determine file's content type
  69. String contentType = null;
  70. try {
  71. contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
  72. } catch (IOException ex) {
  73. log.warn("Could not determine file type.");
  74. }
  75. // Fallback to the default content type if type could not be determined
  76. if(contentType == null) {
  77. contentType = "application/octet-stream";
  78. }
  79. return ResponseEntity.ok()
  80. .contentType(MediaType.parseMediaType(contentType))
  81. .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
  82. .body(resource);
  83. }
  84. }