FileController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.bowintek.practice.controller.system;
  2. import com.bowintek.practice.AppConfig;
  3. import com.bowintek.practice.filter.exception.BaseResponse;
  4. import com.bowintek.practice.filter.exception.RespGenerstor;
  5. import com.bowintek.practice.model.CfFile;
  6. import com.bowintek.practice.services.service.AccountService;
  7. import com.bowintek.practice.services.service.system.FileService;
  8. import com.bowintek.practice.util.BASE64DecodedMultipartFile;
  9. import com.bowintek.practice.vo.system.FileModel;
  10. import io.netty.handler.codec.base64.Base64Encoder;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.*;
  17. import java.net.URLEncoder;
  18. import java.nio.file.Files;
  19. import java.nio.file.Path;
  20. import java.nio.file.Paths;
  21. import java.util.Base64;
  22. import java.util.List;
  23. @RestController
  24. @RequestMapping("/api/system/file")
  25. public class FileController {
  26. @Autowired
  27. private FileService fileService;
  28. @Autowired
  29. private AccountService accountService;
  30. @Autowired
  31. private AppConfig appConfig;
  32. @GetMapping("getList")
  33. public BaseResponse<List<FileModel>> getList(@RequestParam("fileRefID") String fileRefID) {
  34. return RespGenerstor.success(fileService.getList(fileRefID));
  35. }
  36. @PostMapping("upload")
  37. public BaseResponse<Integer> upload(MultipartFile[] files, String fileRefId, Integer fileType, Integer isSingle) throws Exception {
  38. return RespGenerstor.success(fileService.uploadToLocal(files[0], fileRefId, fileType, accountService.getLoginUserID(), isSingle));
  39. }
  40. @PostMapping("uploadBase64")
  41. public BaseResponse<Integer> uploadBase64(@RequestParam(required = false) String base64Str,
  42. @RequestParam(required = false) String fileRefId,
  43. @RequestParam(required = false) Integer fileType,
  44. @RequestParam(required = false) Integer isSingle) throws Exception {
  45. MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64Str);
  46. MultipartFile[] files = new MultipartFile[]{multipartFile};
  47. return RespGenerstor.success(fileService.saveFile(files, fileRefId, fileType, accountService.getLoginUserID(), isSingle));
  48. }
  49. @GetMapping("downFile")
  50. public void downFile(HttpServletResponse response, @RequestParam("fileId") String fileId) throws IOException {
  51. OutputStream os = response.getOutputStream();
  52. fileService.downFtpFile(fileId, os);
  53. if (null != os) {
  54. os.flush();
  55. os.close();
  56. }
  57. }
  58. @GetMapping("downloadTempFile")
  59. public void downloadTempFile(HttpServletResponse response, @RequestParam("fileName") String fileName) throws IOException {
  60. OutputStream os = response.getOutputStream();
  61. fileService.downloadTempFile(fileName, os);
  62. if (null != os) {
  63. os.flush();
  64. os.close();
  65. }
  66. }
  67. @GetMapping("delete")
  68. public BaseResponse<Integer> delete(@RequestParam("fileId") String fileId) throws IOException {
  69. return RespGenerstor.success(fileService.delete(fileId));
  70. }
  71. @GetMapping("getFileBase64")
  72. public BaseResponse<String> getFileBase64(HttpServletResponse response, @RequestParam("fileId") String fileId) throws IOException {
  73. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  74. fileService.downFtpFile(fileId, bos);
  75. byte[] bytes = bos.toByteArray();
  76. Base64.Encoder encoder = Base64.getEncoder();
  77. String base64String = encoder.encodeToString(bytes);
  78. bos.close();
  79. return RespGenerstor.success(base64String);
  80. }
  81. @GetMapping("getFileByte")
  82. public BaseResponse<String> getFileByte(HttpServletResponse response, @RequestParam("fileId") String fileId) throws IOException {
  83. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  84. fileService.downFtpFile(fileId, bos);
  85. bos.close();
  86. return RespGenerstor.success(bos.toByteArray());
  87. }
  88. @GetMapping("downFileToLocal")
  89. public BaseResponse<String> downFileToLocal(@RequestParam("fileId") String fileId) throws IOException {
  90. return RespGenerstor.success(fileService.downFtpFileToLocal(fileId));
  91. }
  92. @GetMapping("downFileToUrl/{fileId}")
  93. public void downFileToUrl(HttpServletResponse response, HttpServletRequest request, @PathVariable("fileId") String fileId) throws IOException {
  94. OutputStream os = response.getOutputStream();
  95. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  96. try {
  97. fileService.downFtpFile(fileId, bos);
  98. CfFile file = fileService.getFileByID(fileId);
  99. response.setContentType("application/force-download");// 设置强制下载不打开
  100. response.setContentType("multipart/form-data;charset=UTF-8");
  101. response.setHeader("Content-Length", String.valueOf(bos.toByteArray().length));
  102. response.addHeader("Content-Disposition",
  103. "attachment;fileName=" + URLEncoder.encode(file.getFileName(), "UTF-8"));// 设置文件名
  104. os.write(bos.toByteArray());
  105. } catch (Exception ex) {
  106. }
  107. bos.close();
  108. if (null != os) {
  109. os.flush();
  110. os.close();
  111. }
  112. }
  113. }