CommonController.java 3.9 KB

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