CommonController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.willalp.web.controller.common;
  2. import com.willalp.common.config.WillalpConfig;
  3. import com.willalp.common.constant.Constants;
  4. import com.willalp.common.core.domain.AjaxResult;
  5. import com.willalp.common.exception.file.FileNameLengthLimitExceededException;
  6. import com.willalp.common.utils.StringUtils;
  7. import com.willalp.common.utils.file.FileUploadUtils;
  8. import com.willalp.common.utils.file.FileUtils;
  9. import com.willalp.framework.config.ServerConfig;
  10. import com.willalp.oss.utils.AttachmentHelper;
  11. import org.apache.commons.fileupload.FileUploadBase;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.web.bind.annotation.*;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.io.IOException;
  21. /**
  22. * 通用请求处理
  23. *
  24. * @author willalp
  25. */
  26. @RestController
  27. public class CommonController {
  28. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
  29. @Autowired
  30. private ServerConfig serverConfig;
  31. @Autowired
  32. private AttachmentHelper attachmentHelper;
  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. try {
  42. if (!FileUtils.checkAllowDownload(fileName)) {
  43. throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
  44. }
  45. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  46. String filePath = WillalpConfig.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. FileUtils.deleteFile(filePath);
  52. }
  53. } catch (Exception e) {
  54. log.error("下载文件失败", e);
  55. }
  56. }
  57. /**
  58. * 通用上传请求
  59. */
  60. @PostMapping("/common/upload")
  61. public AjaxResult uploadFile(MultipartFile file) throws Exception {
  62. try {
  63. // 上传文件路径
  64. String filePath = WillalpConfig.getUploadPath();
  65. // 上传并返回新文件名称
  66. String fileName = file.getOriginalFilename();
  67. String url = serverConfig.getUrl() + FileUploadUtils.upload(filePath, file);
  68. AjaxResult ajax = AjaxResult.success();
  69. ajax.put("fileName", fileName);
  70. ajax.put("url", url);
  71. return ajax;
  72. } catch (Exception e) {
  73. return AjaxResult.error(e.getMessage());
  74. }
  75. }
  76. /**
  77. * 本地资源通用下载
  78. */
  79. @GetMapping("/common/download/resource")
  80. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
  81. throws Exception {
  82. try {
  83. if (!FileUtils.checkAllowDownload(resource)) {
  84. throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
  85. }
  86. // 本地资源路径
  87. String localPath = WillalpConfig.getProfile();
  88. // 数据库资源地址
  89. String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
  90. // 下载名称
  91. String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
  92. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  93. FileUtils.setAttachmentResponseHeader(response, downloadName);
  94. FileUtils.writeBytes(downloadPath, response.getOutputStream());
  95. } catch (Exception e) {
  96. log.error("下载文件失败", e);
  97. }
  98. }
  99. @RequestMapping(value = "/common/ossUpload", method = RequestMethod.POST)
  100. public AjaxResult upload(HttpServletRequest request, MultipartFile file, @RequestParam(required = false, defaultValue = "") String dir) {
  101. try {
  102. return AjaxResult.success((Object) attachmentHelper.upload(request, file, dir));
  103. } catch (IOException e) {
  104. return AjaxResult.error("上传失败");
  105. } catch (FileUploadBase.FileSizeLimitExceededException e) {
  106. return AjaxResult.error("上传失败");
  107. } catch (FileNameLengthLimitExceededException e) {
  108. return AjaxResult.error("上传失败");
  109. } catch (com.willalp.oss.exception.FileNameLengthLimitExceededException e) {
  110. return AjaxResult.error("上传失败");
  111. } catch (com.willalp.oss.exception.InvalidExtensionException e) {
  112. return AjaxResult.error("上传失败");
  113. }
  114. }
  115. }