CommonController.java 4.1 KB

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