CommonController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package com.ylx.web.controller.common;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import cn.hutool.crypto.digest.DigestUtil;
  8. import com.alibaba.fastjson.JSON;
  9. import com.ylx.massage.domain.TbFile;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import com.ylx.common.config.RuoYiConfig;
  22. import com.ylx.common.constant.Constants;
  23. import com.ylx.common.core.domain.AjaxResult;
  24. import com.ylx.common.utils.StringUtils;
  25. import com.ylx.common.utils.file.FileUploadUtils;
  26. import com.ylx.common.utils.file.FileUtils;
  27. import com.ylx.framework.config.ServerConfig;
  28. import com.ylx.massage.service.TbFileService;
  29. import com.ylx.massage.service.SensitiveWordService;
  30. /**
  31. * 通用请求处理
  32. *
  33. * @author ylx
  34. */
  35. @RestController
  36. @RequestMapping("/common")
  37. @Api(tags = {"通用接口"})
  38. public class CommonController {
  39. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
  40. @Autowired
  41. private ServerConfig serverConfig;
  42. @Autowired
  43. private TbFileService fileService;
  44. @Autowired
  45. private SensitiveWordService sensitiveWordService;
  46. private static final String FILE_DELIMETER = ",";
  47. /**
  48. * 通用下载请求
  49. *
  50. * @param fileName 文件名称
  51. * @param delete 是否删除
  52. */
  53. @GetMapping("/download")
  54. public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
  55. try {
  56. if (!FileUtils.checkAllowDownload(fileName)) {
  57. throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
  58. }
  59. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  60. String filePath = RuoYiConfig.getDownloadPath() + fileName;
  61. log.info("下载文件路径:{}", filePath);
  62. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  63. FileUtils.setAttachmentResponseHeader(response, realFileName);
  64. FileUtils.writeBytes(filePath, response.getOutputStream());
  65. if (delete) {
  66. FileUtils.deleteFile(filePath);
  67. }
  68. } catch (Exception e) {
  69. log.error("下载文件失败", e);
  70. }
  71. }
  72. /**
  73. * 通用上传请求(单个)
  74. *
  75. * @param file 上传的文件
  76. * @return AjaxResult 结果
  77. */
  78. @ApiOperation("通用上传请求(单个)")
  79. @PostMapping("/upload")
  80. public AjaxResult uploadFile(MultipartFile file) throws Exception {
  81. return fileService.uploadFile(file);
  82. }
  83. /**
  84. * 通用上传请求(单个)
  85. */
  86. @ApiOperation("通用上传视频请求(单个)")
  87. @PostMapping("/uploadVi")
  88. public AjaxResult uploadFileVi(MultipartFile video) throws Exception {
  89. return fileService.uploadFile(video);
  90. }
  91. /**
  92. * 通用上传请求(多个)
  93. */
  94. @ApiOperation("通用上传请求(多个)")
  95. @PostMapping("/uploads")
  96. public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
  97. try {
  98. // 上传文件路径
  99. String filePath = RuoYiConfig.getUploadPath();
  100. List<String> urls = new ArrayList<String>();
  101. List<String> fileNames = new ArrayList<String>();
  102. List<String> newFileNames = new ArrayList<String>();
  103. List<String> originalFilenames = new ArrayList<String>();
  104. for (MultipartFile file : files) {
  105. // 上传并返回新文件名称
  106. String fileName = FileUploadUtils.upload(filePath, file);
  107. String url = serverConfig.getUrl() + fileName;
  108. urls.add(url);
  109. fileNames.add(fileName);
  110. newFileNames.add(FileUtils.getName(fileName));
  111. originalFilenames.add(file.getOriginalFilename());
  112. }
  113. AjaxResult ajax = AjaxResult.success();
  114. ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
  115. ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
  116. ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
  117. ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
  118. return ajax;
  119. } catch (Exception e) {
  120. return AjaxResult.error(e.getMessage());
  121. }
  122. }
  123. /**
  124. * 本地资源通用下载
  125. */
  126. @GetMapping("/download/resource")
  127. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
  128. throws Exception {
  129. try {
  130. if (!FileUtils.checkAllowDownload(resource)) {
  131. throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
  132. }
  133. // 本地资源路径
  134. String localPath = RuoYiConfig.getProfile();
  135. // 数据库资源地址
  136. String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
  137. // 下载名称
  138. String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
  139. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  140. FileUtils.setAttachmentResponseHeader(response, downloadName);
  141. FileUtils.writeBytes(downloadPath, response.getOutputStream());
  142. } catch (Exception e) {
  143. log.error("下载文件失败", e);
  144. }
  145. }
  146. /**
  147. * 上传Excel文件并导入敏感词数据到数据库
  148. * <p>
  149. * 上传Excel文件并导入敏感词数据到数据库。
  150. * 支持的文件格式:.xls, .xlsx
  151. * Excel文件格式要求:
  152. * - 第一行:表头(敏感词、分类、等级、状态、备注)
  153. * - 第二行起:数据行
  154. * </p>
  155. *
  156. * @param file Excel文件
  157. * @return AjaxResult 导入结果,包含导入统计信息
  158. */
  159. @ApiOperation("上传Excel文件并导入敏感词数据到数据库")
  160. @PostMapping("/importSensitiveWords")
  161. public AjaxResult importSensitiveWords(MultipartFile file) {
  162. try {
  163. // 上传文件
  164. AjaxResult ajaxResult = fileService.uploadFile(file);
  165. log.info("上传文件结果:{}", JSON.toJSONString(ajaxResult));
  166. int code = (int) ajaxResult.get("code");
  167. // 检查上传是否成功
  168. if (code != 200) {
  169. return ajaxResult;
  170. }
  171. log.info("开始导入敏感词Excel文件,文件名:{}", file.getOriginalFilename());
  172. // 调用服务层导入数据
  173. String result = sensitiveWordService.importFromExcel(file);
  174. log.info("敏感词Excel文件导入完成:{}", result);
  175. return AjaxResult.success(result);
  176. } catch (Exception e) {
  177. log.error("导入敏感词Excel文件失败", e);
  178. return AjaxResult.error("导入失败:" + e.getMessage());
  179. }
  180. }
  181. /**
  182. * 导出敏感词数据到Excel文件
  183. * <p>
  184. * 从数据库查询所有敏感词数据,生成Excel文件并下载。
  185. * 文件格式:.xls
  186. * 文件命名:全量敏感词库_当前日期时间.xls(如:全量敏感词库_20240106153045.xls)
  187. * </p>
  188. *
  189. * @param response HTTP响应对象
  190. */
  191. @ApiOperation("导出敏感词数据到Excel文件")
  192. @GetMapping("/exportSensitiveWords")
  193. public void exportSensitiveWords(HttpServletResponse response) {
  194. try {
  195. log.info("开始导出敏感词Excel文件");
  196. // 调用服务层导出数据
  197. sensitiveWordService.exportToExcel(response);
  198. log.info("敏感词Excel文件导出成功");
  199. } catch (Exception e) {
  200. log.error("导出敏感词Excel文件失败", e);
  201. // 设置错误响应
  202. response.setContentType("application/json;charset=UTF-8");
  203. response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  204. try {
  205. response.getWriter().write("{\"code\":500,\"msg\":\"导出失败:" + e.getMessage() + "\"}");
  206. } catch (IOException ex) {
  207. log.error("写入错误响应失败", ex);
  208. }
  209. }
  210. }
  211. /**
  212. * 删除文件
  213. *
  214. * @param fileName 文件名称
  215. * @param delete 是否删除
  216. */
  217. @GetMapping("/delete")
  218. public void filedelete(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
  219. try {
  220. if (!FileUtils.checkAllowDownload(fileName)) {
  221. throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
  222. }
  223. // 本地资源路径
  224. String localPath = RuoYiConfig.getProfile();
  225. // 数据库资源地址
  226. String downloadPath = localPath + StringUtils.substringAfter(fileName, Constants.RESOURCE_PREFIX);
  227. if (delete) {
  228. FileUtils.deleteFile(downloadPath);
  229. }
  230. } catch (Exception e) {
  231. log.error("下载文件失败", e);
  232. }
  233. }
  234. }