CommonController.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.sooka.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.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import com.sooka.common.config.Global;
  13. import com.sooka.common.config.ServerConfig;
  14. import com.sooka.common.constant.Constants;
  15. import com.sooka.common.core.domain.AjaxResult;
  16. import com.sooka.common.utils.StringUtils;
  17. import com.sooka.common.utils.file.FileUploadUtils;
  18. import com.sooka.common.utils.file.FileUtils;
  19. /**
  20. * 通用请求处理
  21. *
  22. * @author lei_wang
  23. */
  24. @Controller
  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.isValidFilename(fileName))
  42. {
  43. throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
  44. }
  45. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  46. String filePath = Global.getDownloadPath() + fileName;
  47. response.setCharacterEncoding("utf-8");
  48. response.setContentType("multipart/form-data");
  49. response.setHeader("Content-Disposition",
  50. "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
  51. FileUtils.writeBytes(filePath, response.getOutputStream());
  52. if (delete)
  53. {
  54. FileUtils.deleteFile(filePath);
  55. }
  56. }
  57. catch (Exception e)
  58. {
  59. log.error("下载文件失败", e);
  60. }
  61. }
  62. /**
  63. * 通用上传请求
  64. */
  65. @PostMapping("/common/upload")
  66. @ResponseBody
  67. public AjaxResult uploadFile(MultipartFile file) throws Exception
  68. {
  69. try
  70. {
  71. // 上传文件路径
  72. String filePath = Global.getUploadPath();
  73. // 上传并返回新文件名称
  74. String fileName = FileUploadUtils.upload(filePath, file);
  75. String url = serverConfig.getUrl() + fileName;
  76. AjaxResult ajax = AjaxResult.success();
  77. ajax.put("fileName", fileName);
  78. ajax.put("url", url);
  79. return ajax;
  80. }
  81. catch (Exception e)
  82. {
  83. e.printStackTrace();
  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. // 本地资源路径
  95. String localPath = Global.getProfile();
  96. // 数据库资源地址
  97. String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
  98. // 下载名称
  99. String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
  100. response.setCharacterEncoding("utf-8");
  101. response.setContentType("multipart/form-data");
  102. response.setHeader("Content-Disposition",
  103. "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
  104. FileUtils.writeBytes(downloadPath, response.getOutputStream());
  105. }
  106. }