CommonController.java 5.9 KB

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