CommonController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.utils.file.FileUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.File;
  14. /**
  15. * 通用请求处理
  16. *
  17. * @author ruoyi
  18. */
  19. @RestController
  20. public class CommonController
  21. {
  22. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
  23. /**
  24. * 通用下载请求
  25. *
  26. * @param fileName 文件名称
  27. * @param delete 是否删除
  28. */
  29. @GetMapping("common/download")
  30. public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
  31. {
  32. try
  33. {
  34. if (!FileUtils.checkAllowDownload(fileName))
  35. {
  36. throw new Exception(StrUtil.format("文件名称({})非法,不允许下载。 ", fileName));
  37. }
  38. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  39. String filePath = RuoYiConfig.getDownloadPath() + fileName;
  40. File file = new File(filePath);
  41. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  42. FileUtils.setAttachmentResponseHeader(response, realFileName);
  43. FileUtils.writeToStream(file, response.getOutputStream());
  44. if (delete)
  45. {
  46. FileUtils.del(file);
  47. }
  48. }
  49. catch (Exception e)
  50. {
  51. log.error("下载文件失败", e);
  52. }
  53. }
  54. /**
  55. * 本地资源通用下载
  56. */
  57. @GetMapping("/common/download/resource")
  58. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
  59. throws Exception
  60. {
  61. try
  62. {
  63. if (!FileUtils.checkAllowDownload(resource))
  64. {
  65. throw new Exception(StrUtil.format("资源文件({})非法,不允许下载。 ", resource));
  66. }
  67. // 本地资源路径
  68. String localPath = RuoYiConfig.getProfile();
  69. // 数据库资源地址
  70. String downloadPath = localPath + StrUtil.subAfter(resource, Constants.RESOURCE_PREFIX,false);
  71. // 下载名称
  72. String downloadName = StrUtil.subAfter(downloadPath, "/",true);
  73. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  74. File file = new File(downloadPath);
  75. FileUtils.setAttachmentResponseHeader(response, downloadName);
  76. FileUtils.writeToStream(file, response.getOutputStream());
  77. }
  78. catch (Exception e)
  79. {
  80. log.error("下载文件失败", e);
  81. }
  82. }
  83. }