CommonController.java 4.6 KB

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