SystemFileController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package com.steerinfo.dil.controller;
  2. import com.google.common.collect.Iterators;
  3. import com.steerinfo.dil.mapper.SystemFileMapper;
  4. import com.steerinfo.dil.util.FtpFileUtil;
  5. import com.steerinfo.dil.util.IDutils;
  6. import com.steerinfo.framework.controller.BaseRESTfulController;
  7. import com.steerinfo.framework.controller.RESTfulResult;
  8. import com.steerinfo.framework.service.pagehelper.PageList;
  9. import com.steerinfo.framework.utils.collection.ListUtils;
  10. import com.steerinfo.dil.model.SystemFile;
  11. import com.steerinfo.dil.service.ISystemFileService;
  12. import io.swagger.annotations.ApiImplicitParam;
  13. import io.swagger.annotations.ApiImplicitParams;
  14. import io.swagger.annotations.ApiOperation;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.apache.shiro.authz.annotation.RequiresPermissions;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.web.bind.annotation.*;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import org.springframework.web.multipart.MultipartRequest;
  21. import javax.annotation.Resource;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.text.SimpleDateFormat;
  25. import java.util.*;
  26. import java.math.BigDecimal;
  27. /**
  28. * SystemFile RESTful接口:
  29. *
  30. * @author generator
  31. * @version 1.0-SNAPSHORT 2023-10-30 09:19
  32. * 类描述
  33. * 修订历史:
  34. * 日期:2023-10-30
  35. * 作者:generator
  36. * 参考:
  37. * 描述:SystemFile RESTful接口
  38. * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
  39. * @see null
  40. */
  41. @RestController
  42. @RequestMapping("/${api.version}/systemfiles")
  43. public class SystemFileController extends BaseRESTfulController {
  44. @Autowired
  45. ISystemFileService systemFileService;
  46. @Resource
  47. SystemFileMapper systemFileMapper;
  48. @Autowired
  49. private FtpFileUtil ftpFileUtil;
  50. //同一个信息包含多个上传文件信息
  51. @PostMapping("/insertFile")
  52. public RESTfulResult insertFile(String fileuuid, @ModelAttribute MultipartFile[] file) {
  53. String filenames = "";
  54. for (int i = 0; i < file.length; i++) {
  55. filenames += file[i].getOriginalFilename() + ";";
  56. }
  57. String filesid = "";
  58. if (file != null) {
  59. for (int i = 0; i < file.length; i++) {
  60. try {
  61. //获取系统时间
  62. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  63. //获取文件名
  64. String oldName = file[i].getOriginalFilename();
  65. //取当前时间的长整形值包含毫秒
  66. String newName = IDutils.getImageName();
  67. //重新命名文件
  68. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  69. String filePath = simpleDateFormat.format(new Date());
  70. //获取输入流
  71. InputStream inputStream = file[i].getInputStream();
  72. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  73. inputStream.close();
  74. if (result) {
  75. SystemFile uploadFile = new SystemFile();
  76. uploadFile.setFilename(oldName);
  77. uploadFile.setFilepath(filePath + "/" + newName);
  78. uploadFile.setId(fileuuid);
  79. SystemFile modela = systemFileService.add(uploadFile);
  80. if (modela != null) {
  81. filesid += "," + modela.getId();
  82. }
  83. } else {
  84. return failed(null, "上传文件失败");
  85. }
  86. } catch (Exception e) {
  87. e.getMessage();
  88. }
  89. }
  90. return success(filesid);
  91. } else {
  92. return failed();
  93. }
  94. }
  95. @PostMapping("/insertFiles2")
  96. public RESTfulResult insertFiles2(String fileuuid, @ModelAttribute MultipartFile[] file) {
  97. String filenames = "";
  98. for (int i = 0; i < file.length; i++) {
  99. filenames += file[i].getOriginalFilename() + ";";
  100. }
  101. String filesid = "";
  102. String[] fileuuids = fileuuid.split(",");
  103. if (file != null) {
  104. for (int i = 0; i < file.length; i++) {
  105. try {
  106. //获取系统时间
  107. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  108. //获取文件名
  109. String oldName = file[i].getOriginalFilename();
  110. //取当前时间的长整形值包含毫秒
  111. String newName = IDutils.getImageName();
  112. //重新命名文件
  113. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  114. String filePath = simpleDateFormat.format(new Date());
  115. //获取输入流
  116. InputStream inputStream = file[i].getInputStream();
  117. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  118. inputStream.close();
  119. if (result) {
  120. SystemFile uploadFile = new SystemFile();
  121. uploadFile.setFilename(oldName);
  122. uploadFile.setFilepath(filePath + "/" + newName);
  123. uploadFile.setId(fileuuids[i]);
  124. SystemFile modela = systemFileService.add(uploadFile);
  125. if (modela != null) {
  126. filesid += "," + modela.getId();
  127. }
  128. } else {
  129. return failed(null, "上传文件失败");
  130. }
  131. } catch (Exception e) {
  132. e.getMessage();
  133. }
  134. }
  135. return success(filesid);
  136. } else {
  137. return failed();
  138. }
  139. }
  140. @PostMapping("/insertFiles")
  141. public RESTfulResult insertFiles(String[] fileUuids, String[] fileNames, String[] fileTypes, Map<Object, MultipartFile> FileList, MultipartRequest request) throws Exception {
  142. List<MultipartFile> files = new ArrayList<>();
  143. int index=-1;
  144. for(int j = 0 ; j < fileTypes.length ; j ++ ) {
  145. for(int i=0;i<21;i++) {
  146. MultipartFile file = request.getFile("file" +j +"" + i);
  147. if (file != null) {
  148. files.add(file);
  149. if (index < 0) {
  150. index = i;
  151. }
  152. }
  153. }
  154. }
  155. if(files.size() == 0) {
  156. return failed("传输失败");
  157. }
  158. for (int i = 0; i < fileUuids.length; i++) {
  159. //获取系统时间
  160. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  161. //获取文件名
  162. String oldName = fileNames[i].toString();
  163. //取当前时间的长整形值包含毫秒
  164. String newName = IDutils.getImageName();
  165. //重新命名文件
  166. String filePath = simpleDateFormat.format(new Date()) + "/" + fileTypes[i].toString();
  167. //获取输入流
  168. String fileTypeName = files.get(i).getOriginalFilename();
  169. fileTypeName = fileTypeName.substring(fileTypeName.lastIndexOf("."));
  170. newName = oldName + "(" + newName + ")" + fileTypeName;
  171. InputStream inputStream = files.get(i).getInputStream();
  172. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  173. inputStream.close();
  174. if (result) {
  175. SystemFile uploadFile = new SystemFile();
  176. uploadFile.setFilename(oldName);
  177. uploadFile.setFilepath(filePath + "/" + newName);
  178. uploadFile.setId(fileUuids[i]);
  179. SystemFile modela = systemFileService.add(uploadFile);
  180. if (modela != null) {
  181. fileUuids[i] += "," + modela.getId();
  182. }
  183. } else {
  184. return failed(null, "上传文件失败");
  185. }
  186. }
  187. return success();
  188. }
  189. @PostMapping("/previewfile")
  190. public RESTfulResult previewfile(@RequestBody HashMap parmas) {
  191. String filename = parmas.get("filename").toString();
  192. String filepath = parmas.get("filepath").toString();
  193. if (filename == null || filename.isEmpty()) {
  194. return failed("该图片不存在!");
  195. }
  196. if (filepath == null || filepath.isEmpty()) {
  197. return failed("该图片地址不存在!");
  198. }
  199. try {
  200. String result = ftpFileUtil.downloadFile(filename, filepath);
  201. return success(result);
  202. } catch (IOException e) {
  203. e.getMessage();
  204. return failed();
  205. }
  206. }
  207. @PostMapping("/previewfile2")
  208. public RESTfulResult previewfile2(@RequestBody HashMap parmas) {
  209. SystemFile value = systemFileMapper.selectByPrimaryKey(parmas.get("id").toString());
  210. String fileName = value.getFilename();
  211. String filepath = value.getFilepath();
  212. if (fileName == null || fileName.isEmpty()) {
  213. return failed("该图片不存在!");
  214. }
  215. if (filepath == null || filepath.isEmpty()) {
  216. return failed("该图片地址不存在!");
  217. }
  218. try {
  219. String result = ftpFileUtil.downloadFile(fileName, filepath);
  220. return success(result);
  221. } catch (IOException e) {
  222. e.getMessage();
  223. return failed();
  224. }
  225. }
  226. @PostMapping("/updateFile")
  227. public RESTfulResult updateFile(String alternateFields1, @ModelAttribute MultipartFile[] file) {
  228. systemFileService.delete(alternateFields1);
  229. String filenames = "";
  230. for (int i = 0; i < file.length; i++) {
  231. filenames += file[i].getOriginalFilename() + ";";
  232. }
  233. String filesid = "";
  234. if (file != null) {
  235. for (int i = 0; i < file.length; i++) {
  236. try {
  237. //获取系统时间
  238. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  239. //获取文件名
  240. String oldName = file[i].getOriginalFilename();
  241. //取当前时间的长整形值包含毫秒
  242. String newName = IDutils.getImageName();
  243. //重新命名文件
  244. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  245. String filePath = simpleDateFormat.format(new Date());
  246. //获取输入流
  247. InputStream inputStream = file[i].getInputStream();
  248. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  249. inputStream.close();
  250. if (result) {
  251. SystemFile uploadFile = new SystemFile();
  252. uploadFile.setFilename(oldName);
  253. uploadFile.setFilepath(filePath + "/" + newName);
  254. uploadFile.setId(alternateFields1);
  255. SystemFile modela = systemFileService.add(uploadFile);
  256. if (modela != null) {
  257. filesid += "," + modela.getId();
  258. }
  259. } else {
  260. return failed(null, "上传文件失败");
  261. }
  262. } catch (Exception e) {
  263. e.getMessage();
  264. }
  265. }
  266. return success(filesid);
  267. } else {
  268. return failed();
  269. }
  270. }
  271. @PostMapping("/previewfileList")
  272. public RESTfulResult previewfileList(@RequestBody HashMap map) {
  273. String uuid = map.get("uuid").toString();
  274. String[] uuidList = uuid.split(";");
  275. //遍历数组,去找文件名称和路径
  276. List<Map<String,Object>> mapList = systemFileMapper.getFileInfo(uuidList);
  277. List<Map<String,Object>> resultList = new ArrayList<>();
  278. for (Map<String,Object> parmas : mapList) {
  279. String filename = parmas.get("FILENAME").toString();
  280. String filepath = parmas.get("FILEPATH").toString();
  281. if (filename == null ||filename.isEmpty()) {
  282. return failed("该图片不存在!");
  283. }
  284. if (filepath == null ||filepath.isEmpty()) {
  285. return failed("该图片地址不存在!");
  286. }
  287. try {
  288. Map<String,Object> result = ftpFileUtil.downloadFileNew(filename, filepath);
  289. resultList.add(result);
  290. } catch (IOException e) {
  291. e.getMessage();
  292. continue;
  293. }
  294. }
  295. return success(resultList);
  296. }
  297. }