CommonController.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.ozs.web.controller.common;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import com.ozs.system.domain.SysFileInfo;
  8. import com.ozs.system.service.SysFileService;
  9. import io.swagger.annotations.Api;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import com.ozs.common.config.PurchaseConfig;
  20. import com.ozs.common.constant.Constants;
  21. import com.ozs.common.core.domain.AjaxResult;
  22. import com.ozs.common.utils.StringUtils;
  23. import com.ozs.common.utils.file.FileUploadUtils;
  24. import com.ozs.common.utils.file.FileUtils;
  25. import com.ozs.framework.config.ServerConfig;
  26. import static com.ozs.common.utils.SecurityUtils.getUserId;
  27. /**
  28. * 通用请求处理
  29. *
  30. * @author ruoyi
  31. */
  32. @Api(tags = "统用功能")
  33. @RestController
  34. @RequestMapping("/common")
  35. public class CommonController {
  36. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
  37. @Autowired
  38. private ServerConfig serverConfig;
  39. @Autowired
  40. private SysFileService sysFileService;
  41. private static final String FILE_DELIMETER = ",";
  42. /**
  43. * 通用下载请求
  44. *
  45. * @param fileName 文件名称
  46. * @param delete 是否删除
  47. */
  48. @GetMapping("/download")
  49. public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
  50. try {
  51. if (!FileUtils.checkAllowDownload(fileName)) {
  52. throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
  53. }
  54. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  55. String filePath = PurchaseConfig.getDownloadPath() + fileName;
  56. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  57. FileUtils.setAttachmentResponseHeader(response, realFileName);
  58. FileUtils.writeBytes(filePath, response.getOutputStream());
  59. if (delete) {
  60. FileUtils.deleteFile(filePath);
  61. }
  62. } catch (Exception e) {
  63. log.error("下载文件失败", e);
  64. }
  65. }
  66. /**
  67. * 通用上传请求(单个)
  68. */
  69. @PostMapping("/upload")
  70. public AjaxResult uploadFile(MultipartFile file) throws Exception {
  71. try {
  72. // 上传文件路径
  73. String filePath = PurchaseConfig.getUploadPath();
  74. // 上传并返回新文件名称
  75. String fileName = FileUploadUtils.upload(filePath, file);
  76. String url = serverConfig.getUrl() + fileName;
  77. SysFileInfo filedto = SysFileInfo.builder()
  78. .fileUrl(url)
  79. .fileName(file.getOriginalFilename())
  80. .fileMappingPath(fileName)
  81. .fileNewName(FileUtils.getName(fileName))
  82. .build();
  83. filedto.setCreated(getUserId().toString());
  84. filedto.setCreateTime(new Date());
  85. filedto.setUpdated(filedto.getCreated());
  86. filedto.setUpdateTime(filedto.getCreateTime());
  87. boolean save = sysFileService.save(filedto);
  88. return AjaxResult.success(filedto);
  89. } catch (Exception e) {
  90. return AjaxResult.error(e.getMessage());
  91. }
  92. }
  93. /**
  94. * 通用上传请求(多个)
  95. */
  96. @PostMapping("/uploads")
  97. public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
  98. try {
  99. // 上传文件路径
  100. String filePath = PurchaseConfig.getUploadPath();
  101. List<SysFileInfo> list = new ArrayList<>();
  102. for (MultipartFile file : files) {
  103. // // 上传并返回新文件名称
  104. String fileName = FileUploadUtils.upload(filePath, file);
  105. String url = serverConfig.getUrl() + fileName;
  106. SysFileInfo filedto = SysFileInfo.builder()
  107. .fileUrl(url)
  108. .fileName(file.getOriginalFilename())
  109. .fileMappingPath(fileName)
  110. .fileNewName(FileUtils.getName(fileName))
  111. .build();
  112. filedto.setCreated(getUserId().toString());
  113. filedto.setCreateTime(new Date());
  114. filedto.setUpdated(filedto.getCreated());
  115. filedto.setUpdateTime(filedto.getCreateTime());
  116. boolean save = sysFileService.save(filedto);
  117. list.add(filedto);
  118. }
  119. return AjaxResult.success(list);
  120. } catch (Exception e) {
  121. return AjaxResult.error(e.getMessage());
  122. }
  123. }
  124. /**
  125. * 本地资源通用下载
  126. */
  127. @GetMapping("/download/resource")
  128. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
  129. throws Exception {
  130. try {
  131. if (!FileUtils.checkAllowDownload(resource)) {
  132. throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
  133. }
  134. // 本地资源路径
  135. String localPath = PurchaseConfig.getProfile();
  136. // 数据库资源地址
  137. String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
  138. // 下载名称
  139. String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
  140. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  141. FileUtils.setAttachmentResponseHeader(response, downloadName);
  142. FileUtils.writeBytes(downloadPath, response.getOutputStream());
  143. } catch (Exception e) {
  144. log.error("下载文件失败", e);
  145. }
  146. }
  147. }