CommonController.java 8.4 KB

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