CommonController.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.ruoyi.web.controller.common;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.ruoyi.common.config.RuoYiConfig;
  4. import com.ruoyi.common.constant.Constants;
  5. import com.ruoyi.common.core.domain.AjaxResult;
  6. import com.ruoyi.common.utils.file.FileUploadUtils;
  7. import com.ruoyi.common.utils.file.FileUtils;
  8. import com.ruoyi.framework.config.ServerConfig;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.http.MediaType;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.File;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. /**
  23. * 通用请求处理
  24. *
  25. * @author ruoyi
  26. */
  27. @RestController
  28. public class CommonController
  29. {
  30. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
  31. @Autowired
  32. private ServerConfig serverConfig;
  33. /**
  34. * 通用下载请求
  35. *
  36. * @param fileName 文件名称
  37. * @param delete 是否删除
  38. */
  39. @GetMapping("common/download")
  40. public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
  41. {
  42. try
  43. {
  44. if (!FileUtils.checkAllowDownload(fileName))
  45. {
  46. throw new Exception(StrUtil.format("文件名称({})非法,不允许下载。 ", fileName));
  47. }
  48. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  49. String filePath = RuoYiConfig.getDownloadPath() + fileName;
  50. File file = new File(filePath);
  51. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  52. FileUtils.setAttachmentResponseHeader(response, realFileName);
  53. FileUtils.writeToStream(file, response.getOutputStream());
  54. if (delete)
  55. {
  56. FileUtils.del(file);
  57. }
  58. }
  59. catch (Exception e)
  60. {
  61. log.error("下载文件失败", e);
  62. }
  63. }
  64. /**
  65. * 通用上传请求
  66. */
  67. @PostMapping("/common/upload")
  68. public AjaxResult uploadFile(MultipartFile file) throws Exception
  69. {
  70. try
  71. {
  72. // 上传文件路径
  73. String filePath = RuoYiConfig.getUploadPath();
  74. // 上传并返回新文件名称
  75. String fileName = FileUploadUtils.upload(filePath, file);
  76. String url = serverConfig.getUrl() + fileName;
  77. Map<String,Object> ajax = new HashMap<>();
  78. ajax.put("fileName", fileName);
  79. ajax.put("url", url);
  80. return AjaxResult.success(ajax);
  81. }
  82. catch (Exception e)
  83. {
  84. return AjaxResult.error(e.getMessage());
  85. }
  86. }
  87. /**
  88. * 本地资源通用下载
  89. */
  90. @GetMapping("/common/download/resource")
  91. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
  92. throws Exception
  93. {
  94. try
  95. {
  96. if (!FileUtils.checkAllowDownload(resource))
  97. {
  98. throw new Exception(StrUtil.format("资源文件({})非法,不允许下载。 ", resource));
  99. }
  100. // 本地资源路径
  101. String localPath = RuoYiConfig.getProfile();
  102. // 数据库资源地址
  103. String downloadPath = localPath + StrUtil.subAfter(resource, Constants.RESOURCE_PREFIX,false);
  104. // 下载名称
  105. String downloadName = StrUtil.subAfter(downloadPath, "/",true);
  106. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  107. File file = new File(downloadPath);
  108. FileUtils.setAttachmentResponseHeader(response, downloadName);
  109. FileUtils.writeToStream(file, response.getOutputStream());
  110. }
  111. catch (Exception e)
  112. {
  113. log.error("下载文件失败", e);
  114. }
  115. }
  116. }