CommonController.java 4.0 KB

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