CommonController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.utils.file.FileUploadUtils;
  7. import com.zhongzheng.common.utils.file.FileUtils;
  8. import com.zhongzheng.framework.config.ServerConfig;
  9. import com.zhongzheng.modules.order.bo.OrderAddBo;
  10. import io.swagger.annotations.ApiOperation;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. /**
  23. * 通用请求处理
  24. *
  25. * @author zhongzheng
  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. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  51. FileUtils.setAttachmentResponseHeader(response, realFileName);
  52. FileUtils.writeBytes(filePath, response.getOutputStream());
  53. /* if (delete)
  54. {
  55. FileUtils.deleteFile(filePath);
  56. }*/
  57. FileUtils.deleteFile(filePath);
  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. AjaxResult ajax = AjaxResult.success();
  78. ajax.put("fileName", fileName);
  79. ajax.put("url", url);
  80. return 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. FileUtils.setAttachmentResponseHeader(response, downloadName);
  108. FileUtils.writeBytes(downloadPath, response.getOutputStream());
  109. }
  110. catch (Exception e)
  111. {
  112. log.error("下载文件失败", e);
  113. }
  114. }
  115. }