CommonController.java 8.2 KB

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