CommonController.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.zhongzheng.controller;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  5. import com.zhongzheng.common.config.RuoYiConfig;
  6. import com.zhongzheng.common.constant.Constants;
  7. import com.zhongzheng.common.core.controller.BaseController;
  8. import com.zhongzheng.common.core.domain.AjaxResult;
  9. import com.zhongzheng.common.exception.CustomException;
  10. import com.zhongzheng.common.utils.file.FileUploadUtils;
  11. import com.zhongzheng.common.utils.file.FileUtils;
  12. import com.zhongzheng.framework.config.ServerConfig;
  13. import com.zhongzheng.modules.alioss.bo.FileHandleBo;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.http.MediaType;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.File;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.stream.Collectors;
  31. /**
  32. * 通用请求处理
  33. *
  34. * @author zhongzheng
  35. */
  36. @RestController
  37. public class CommonController extends BaseController {
  38. private static final Logger log = LoggerFactory.getLogger(CommonController.class);
  39. @Autowired
  40. private ServerConfig serverConfig;
  41. /**
  42. * 通用下载请求
  43. *
  44. * @param fileName 文件名称
  45. * @param delete 是否删除
  46. */
  47. @GetMapping("common/download")
  48. public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
  49. try {
  50. if (!FileUtils.checkAllowDownload(fileName)) {
  51. throw new Exception(StrUtil.format("文件名称({})非法,不允许下载。 ", fileName));
  52. }
  53. String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
  54. String filePath = RuoYiConfig.getDownloadPath() + fileName;
  55. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  56. FileUtils.setAttachmentResponseHeader(response, realFileName);
  57. FileUtils.writeBytes(filePath, response.getOutputStream());
  58. /* if (delete)
  59. {
  60. FileUtils.deleteFile(filePath);
  61. }*/
  62. FileUtils.deleteFile(filePath);
  63. } catch (Exception e) {
  64. log.error("下载文件失败", e);
  65. }
  66. }
  67. /**
  68. * 通用上传请求
  69. */
  70. @PostMapping("/common/upload")
  71. public AjaxResult uploadFile(MultipartFile file) throws Exception {
  72. try {
  73. // 上传文件路径
  74. String filePath = RuoYiConfig.getUploadPath();
  75. // 上传并返回新文件名称
  76. String fileName = FileUploadUtils.upload(filePath, file);
  77. String url = serverConfig.getUrl() + fileName;
  78. AjaxResult ajax = AjaxResult.success();
  79. ajax.put("fileName", fileName);
  80. ajax.put("url", url);
  81. return ajax;
  82. } catch (Exception e) {
  83. return AjaxResult.error(e.getMessage());
  84. }
  85. }
  86. /**
  87. * 文件分片处理
  88. */
  89. @PostMapping("/common/decompression")
  90. public AjaxResult<List<String>> uploadDecompression(MultipartFile file, String param) throws Exception {
  91. String zhiyuan = System.getProperty("user.dir");
  92. FileHandleBo bo = JSONObject.parseObject(param, FileHandleBo.class);
  93. String path = zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign();
  94. String[] split = bo.getName().split("\\.");
  95. String fileName = bo.getFileMd5() + "_" + bo.getIndex()+"."+ split[split.length - 1];
  96. FileUploadUtils.uploadFragment(path, file, fileName);
  97. File file2 = new File(path);
  98. List<String> fileNameList = Arrays.stream(file2.listFiles()).map(File::getName).collect(Collectors.toList());
  99. return AjaxResult.success(fileNameList);
  100. }
  101. /**
  102. * 本地文件删除
  103. */
  104. @GetMapping("/common/del/file")
  105. public AjaxResult<Void> delFile() {
  106. String zhiyuan = System.getProperty("user.dir");
  107. String toPath = zhiyuan + "/zhongzheng-admin/src/main/resources/zhiyuan";
  108. File directory = new File(toPath);
  109. try {
  110. Files.walk(directory.toPath())
  111. .filter(Files::isRegularFile)
  112. .map(Path::toFile)
  113. .forEach(File::delete);
  114. }catch (Exception e){
  115. e.printStackTrace();
  116. throw new CustomException(e.getMessage());
  117. }
  118. return AjaxResult.success();
  119. }
  120. /**
  121. * 文件分片处理
  122. */
  123. @PostMapping("/common/merge/file")
  124. public AjaxResult uploadDecompression(@RequestBody FileHandleBo bo) throws Exception {
  125. String zhiyuan = System.getProperty("user.dir");
  126. String path = zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign();
  127. //全部上传完成 合并文件返回文件名称
  128. String toPath = zhiyuan + "/zhongzheng-admin/src/main/resources/zhiyuan"+bo.getTimeSign();
  129. File file1 = new File(toPath);
  130. if (!file1.exists()) {
  131. file1.mkdirs();
  132. }
  133. File ToFile = new File(toPath + "/" + bo.getName());
  134. if (ToFile.createNewFile()) {
  135. FileUploadUtils.merge(bo.getFileMd5(), path, ToFile.getPath());
  136. //校验文件MD5值
  137. String content = FileUtils.md5HashCode(ToFile.getPath());
  138. if (!bo.getFileMd5().equals(content)) {
  139. //文件不一致,删除文件
  140. ToFile.delete();
  141. //删除分片资源
  142. FileUtils.deleteFilePackage(zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign());
  143. throw new CustomException("文件上传失败!请重新上传");
  144. }
  145. }
  146. //删除分片资源
  147. FileUtils.deleteFilePackage(zhiyuan + "/zhongzheng-admin/src/main/resources/fenpian"+bo.getTimeSign());
  148. return AjaxResult.success();
  149. }
  150. @PostMapping("/common/delete/file")
  151. public AjaxResult deleteDecompression(@RequestBody FileHandleBo bo) throws Exception {
  152. //删除合并文件
  153. String zhiyuan = System.getProperty("user.dir");
  154. String toPath = zhiyuan + "/zhongzheng-admin/src/main/resources/zhiyuan"+bo.getTimeSign();
  155. File file = new File(toPath);
  156. List<File> files = Arrays.stream(file.listFiles()).collect(Collectors.toList());
  157. if (!CollectionUtils.isEmpty(files)){
  158. files.stream().filter(item -> item.getName().equals(bo.getName())).forEach(x -> {
  159. x.delete();
  160. });
  161. }
  162. return AjaxResult.success();
  163. }
  164. /**
  165. * 本地资源通用下载
  166. */
  167. @GetMapping("/common/download/resource")
  168. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
  169. throws Exception {
  170. try {
  171. if (!FileUtils.checkAllowDownload(resource)) {
  172. throw new Exception(StrUtil.format("资源文件({})非法,不允许下载。 ", resource));
  173. }
  174. // 本地资源路径
  175. String localPath = RuoYiConfig.getProfile();
  176. // 数据库资源地址
  177. String downloadPath = localPath + StrUtil.subAfter(resource, Constants.RESOURCE_PREFIX, false);
  178. // 下载名称
  179. String downloadName = StrUtil.subAfter(downloadPath, "/", true);
  180. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  181. FileUtils.setAttachmentResponseHeader(response, downloadName);
  182. FileUtils.writeBytes(downloadPath, response.getOutputStream());
  183. } catch (Exception e) {
  184. log.error("下载文件失败", e);
  185. }
  186. }
  187. }