SystemFileController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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.util.MultiValueMap;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import org.springframework.web.multipart.MultipartRequest;
  22. import javax.annotation.Resource;
  23. import javax.imageio.ImageIO;
  24. import java.awt.*;
  25. import java.awt.image.BufferedImage;
  26. import java.io.*;
  27. import java.text.SimpleDateFormat;
  28. import java.util.*;
  29. import java.math.BigDecimal;
  30. import java.util.List;
  31. /**
  32. * SystemFile RESTful接口:
  33. *
  34. * @author generator
  35. * @version 1.0-SNAPSHORT 2023-10-30 09:19
  36. * 类描述
  37. * 修订历史:
  38. * 日期:2023-10-30
  39. * 作者:generator
  40. * 参考:
  41. * 描述:SystemFile RESTful接口
  42. * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
  43. * @see null
  44. */
  45. @RestController
  46. @RequestMapping("/${api.version}/systemfiles")
  47. public class SystemFileController extends BaseRESTfulController {
  48. @Autowired
  49. ISystemFileService systemFileService;
  50. @Resource
  51. SystemFileMapper systemFileMapper;
  52. @Autowired
  53. private FtpFileUtil ftpFileUtil;
  54. //同一个信息包含多个上传文件信息
  55. @PostMapping("/insertFile")
  56. public RESTfulResult insertFile(String fileuuid, @ModelAttribute MultipartFile[] file) {
  57. String filenames = "";
  58. for (int i = 0; i < file.length; i++) {
  59. filenames += file[i].getOriginalFilename() + ";";
  60. }
  61. String filesid = "";
  62. if (file != null) {
  63. for (int i = 0; i < file.length; i++) {
  64. try {
  65. //获取系统时间
  66. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  67. //获取文件名
  68. String oldName = file[i].getOriginalFilename();
  69. //取当前时间的长整形值包含毫秒
  70. String newName = IDutils.getImageName();
  71. //重新命名文件
  72. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  73. String filePath = simpleDateFormat.format(new Date());
  74. //获取输入流
  75. InputStream inputStream = file[i].getInputStream();
  76. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  77. inputStream.close();
  78. if (result) {
  79. SystemFile uploadFile = new SystemFile();
  80. uploadFile.setFilename(oldName);
  81. uploadFile.setFilepath(filePath + "/" + newName);
  82. uploadFile.setId(fileuuid);
  83. SystemFile modela = systemFileService.add(uploadFile);
  84. if (modela != null) {
  85. filesid += "," + modela.getId();
  86. }
  87. } else {
  88. return failed(null, "上传文件失败");
  89. }
  90. } catch (Exception e) {
  91. e.getMessage();
  92. }
  93. }
  94. return success(filesid);
  95. } else {
  96. return failed();
  97. }
  98. }
  99. @PostMapping("/insertFiles2")
  100. public RESTfulResult insertFiles2(@RequestParam String fileuuid, @ModelAttribute MultipartFile[] file) {
  101. String filenames = "";
  102. for (int i = 0; i < file.length; i++) {
  103. filenames += file[i].getOriginalFilename() + ";";
  104. }
  105. String filesid = "";
  106. String[] fileuuids = fileuuid.split(",");
  107. if (file != null) {
  108. for (int i = 0; i < file.length; i++) {
  109. try {
  110. //获取系统时间
  111. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  112. //获取文件名
  113. String oldName = file[i].getOriginalFilename();
  114. //取当前时间的长整形值包含毫秒
  115. String newName = IDutils.getImageName();
  116. //重新命名文件
  117. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  118. String filePath = simpleDateFormat.format(new Date());
  119. //获取输入流
  120. InputStream inputStream = file[i].getInputStream();
  121. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  122. inputStream.close();
  123. if (result) {
  124. SystemFile uploadFile = new SystemFile();
  125. uploadFile.setFilename(oldName);
  126. uploadFile.setFilepath(filePath + "/" + newName);
  127. uploadFile.setId(fileuuids[i]);
  128. SystemFile modela = systemFileService.add(uploadFile);
  129. if (modela != null) {
  130. filesid += "," + modela.getId();
  131. }
  132. } else {
  133. return failed(null, "上传文件失败");
  134. }
  135. } catch (Exception e) {
  136. e.getMessage();
  137. }
  138. }
  139. return success(filesid);
  140. } else {
  141. return failed();
  142. }
  143. }
  144. @PostMapping("/insertFiles")
  145. public RESTfulResult insertFiles(String[] fileUuids, String[] fileNames, String[] fileTypes, Map<Object, MultipartFile> FileList, MultipartRequest request) throws Exception {
  146. List<MultipartFile> files = new ArrayList<>();
  147. int index = -1;
  148. for (int j = 0; j < fileTypes.length; j++) {
  149. for (int i = 0; i < 21; i++) {
  150. MultipartFile file = request.getFile("file" + j + "" + i);
  151. if (file != null) {
  152. files.add(file);
  153. if (index < 0) {
  154. index = i;
  155. }
  156. }
  157. }
  158. }
  159. if (files.size() == 0) {
  160. return failed("传输失败");
  161. }
  162. for (int i = 0; i < fileUuids.length; i++) {
  163. //获取系统时间
  164. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  165. //获取文件名
  166. String oldName = fileNames[i].toString();
  167. //取当前时间的长整形值包含毫秒
  168. String newName = IDutils.getImageName();
  169. //重新命名文件
  170. String filePath = simpleDateFormat.format(new Date()) + "/" + fileTypes[i].toString();
  171. //获取输入流
  172. String fileTypeName = files.get(i).getOriginalFilename();
  173. fileTypeName = fileTypeName.substring(fileTypeName.lastIndexOf("."));
  174. newName = oldName + "(" + newName + ")" + fileTypeName;
  175. InputStream inputStream = files.get(i).getInputStream();
  176. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  177. inputStream.close();
  178. if (result) {
  179. SystemFile uploadFile = new SystemFile();
  180. uploadFile.setFilename(oldName);
  181. uploadFile.setFilepath(filePath + "/" + newName);
  182. uploadFile.setId(fileUuids[i]);
  183. SystemFile modela = systemFileService.add(uploadFile);
  184. if (modela != null) {
  185. fileUuids[i] += "," + modela.getId();
  186. }
  187. } else {
  188. return failed(null, "上传文件失败");
  189. }
  190. }
  191. return success();
  192. }
  193. @PostMapping("/previewfile")
  194. public RESTfulResult previewfile(@RequestBody HashMap parmas) {
  195. String filename = parmas.get("filename").toString();
  196. String filepath = parmas.get("filepath").toString();
  197. if (filename == null || filename.isEmpty()) {
  198. return failed("该图片不存在!");
  199. }
  200. if (filepath == null || filepath.isEmpty()) {
  201. return failed("该图片地址不存在!");
  202. }
  203. try {
  204. String result = ftpFileUtil.downloadFile(filename, filepath);
  205. return success(result);
  206. } catch (IOException e) {
  207. e.getMessage();
  208. return failed();
  209. }
  210. }
  211. @PostMapping("/previewfile2")
  212. public RESTfulResult previewfile2(@RequestBody HashMap parmas) {
  213. try {
  214. if (parmas.get("id")==null) {
  215. return failed(1,"当前详单没有图片");
  216. }
  217. SystemFile value = systemFileMapper.selectByPrimaryKey(parmas.get("id").toString());
  218. if (value==null){
  219. return failed(1,"该文件不存在!请查验文件是否被清除或已损坏!");
  220. }
  221. String fileName = value.getFilename();
  222. String filepath = value.getFilepath();
  223. if (fileName == null || fileName.isEmpty()) {
  224. return failed(1,"该文件不存在!");
  225. }
  226. if (filepath == null || filepath.isEmpty()) {
  227. return failed(1,"该文件地址不存在!");
  228. }
  229. String result = ftpFileUtil.downloadFile(fileName, filepath);
  230. return success(result);
  231. } catch (IOException e) {
  232. e.getMessage();
  233. return failed();
  234. }
  235. }
  236. @PostMapping("/updateFile")
  237. public RESTfulResult updateFile(String alternateFields1, @ModelAttribute MultipartFile[] file) {
  238. systemFileService.delete(alternateFields1);
  239. String filenames = "";
  240. for (int i = 0; i < file.length; i++) {
  241. filenames += file[i].getOriginalFilename() + ";";
  242. }
  243. String filesid = "";
  244. if (file != null) {
  245. for (int i = 0; i < file.length; i++) {
  246. try {
  247. //获取系统时间
  248. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  249. //获取文件名
  250. String oldName = file[i].getOriginalFilename();
  251. //取当前时间的长整形值包含毫秒
  252. String newName = IDutils.getImageName();
  253. //重新命名文件
  254. newName = newName + oldName.substring(oldName.lastIndexOf("."));
  255. String filePath = simpleDateFormat.format(new Date());
  256. //获取输入流
  257. InputStream inputStream = file[i].getInputStream();
  258. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  259. inputStream.close();
  260. if (result) {
  261. SystemFile uploadFile = new SystemFile();
  262. uploadFile.setFilename(oldName);
  263. uploadFile.setFilepath(filePath + "/" + newName);
  264. uploadFile.setId(alternateFields1);
  265. SystemFile modela = systemFileService.add(uploadFile);
  266. if (modela != null) {
  267. filesid += "," + modela.getId();
  268. }
  269. } else {
  270. return failed(null, "上传文件失败");
  271. }
  272. } catch (Exception e) {
  273. e.getMessage();
  274. }
  275. }
  276. return success(filesid);
  277. } else {
  278. return failed();
  279. }
  280. }
  281. @PostMapping("/previewfileList")
  282. public RESTfulResult previewfileList(@RequestBody HashMap map) {
  283. String uuid = map.get("uuid").toString();
  284. String[] uuidList = uuid.split(";");
  285. //遍历数组,去找文件名称和路径
  286. List<Map<String, Object>> mapList = systemFileMapper.getFileInfo(uuidList);
  287. List<Map<String, Object>> resultList = new ArrayList<>();
  288. for (Map<String, Object> parmas : mapList) {
  289. String filename = parmas.get("FILENAME").toString();
  290. String filepath = parmas.get("FILEPATH").toString();
  291. if (filename == null || filename.isEmpty()) {
  292. return failed("该图片不存在!");
  293. }
  294. if (filepath == null || filepath.isEmpty()) {
  295. return failed("该图片地址不存在!");
  296. }
  297. try {
  298. Map<String, Object> result = ftpFileUtil.downloadFileNew(filename, filepath);
  299. //如果是返回base64,则给这个;网络路径则给netUrl
  300. result.put("dataType","base");
  301. resultList.add(result);
  302. } catch (IOException e) {
  303. e.getMessage();
  304. continue;
  305. }
  306. }
  307. return success(resultList);
  308. }
  309. @PostMapping("/insertFilesReal")
  310. public RESTfulResult insertFilesReal(MultipartRequest request, String[] uuidTypes) throws Exception {
  311. //List<MultipartFile> files = new ArrayList<>();
  312. //获取多个文件
  313. MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
  314. List<MultipartFile> files = multiFileMap.get("file");
  315. System.out.println(uuidTypes + "uuidTypes");
  316. System.out.println(request);
  317. if (files == null) {
  318. return success();
  319. }
  320. if (files.size() == 0) {
  321. return failed("传输失败");
  322. }
  323. int index = 0;
  324. for (MultipartFile file : files) {
  325. String uuidType = uuidTypes[index];
  326. String fileMediaType = file.getContentType();
  327. String newName = uuidType.split(";")[0];
  328. String uuid = uuidType.split(";")[0];
  329. String fileType = uuidType.split(";")[1];
  330. InputStream inputStream = file.getInputStream();
  331. if (fileMediaType != null && fileMediaType.contains("image")) {
  332. newName = uuidType.split(";")[0] + "." +fileMediaType.split("/")[1];
  333. ////如果是图片
  334. //BufferedImage originalImage = ImageIO.read(inputStream);
  335. //// 确保原始图片有效且可以读取
  336. //if (originalImage != null) {
  337. // // 添加水印逻辑
  338. // inputStream = addWatermark(originalImage);
  339. // // 保存或返回带有水印的图片
  340. // // ...
  341. //}
  342. }
  343. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
  344. String filePath = "/" + fileType + simpleDateFormat.format(new Date());
  345. boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false);
  346. inputStream.close();
  347. if (result) {
  348. SystemFile uploadFile = new SystemFile();
  349. uploadFile.setFilename(newName);
  350. uploadFile.setFilepath(filePath + "/" + newName);
  351. uploadFile.setId(uuid);
  352. SystemFile modela = systemFileService.add(uploadFile);
  353. if (modela != null) {
  354. uuid += "," + modela.getId();
  355. }
  356. } else {
  357. return failed(null, "上传文件失败");
  358. }
  359. index++;
  360. }
  361. return success();
  362. }
  363. //private InputStream addWatermark(BufferedImage originalImage) {
  364. // int width = originalImage.getWidth();
  365. // int height = originalImage.getHeight();
  366. //
  367. // //BufferedImage watermarkedImage = Scalr.resize(originalImage, width, height, Scalr.Mode.FIT_EXACT);
  368. //
  369. // Graphics2D graphics = watermarkedImage.createGraphics();
  370. //
  371. // // 设置水印样式
  372. // graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  373. // Font font = new Font("Arial", Font.BOLD, 30);
  374. // graphics.setFont(font);
  375. // Color color = new Color(255, 0, 0, 128);
  376. // graphics.setColor(color);
  377. //
  378. // // 添加水印文字
  379. // graphics.drawString("Your Watermark Text", 10, height - 40); // 示例位置
  380. //
  381. // graphics.dispose();
  382. //
  383. // // 将带水印的图片转为InputStream
  384. // ByteArrayOutputStream baos = new ByteArrayOutputStream();
  385. // ImageIO.write(watermarkedImage, "jpg", baos);
  386. // InputStream watermarkedStream = new ByteArrayInputStream(baos.toByteArray());
  387. //
  388. // return watermarkedStream;
  389. //}
  390. //读取文件
  391. @PostMapping("/readExcel")
  392. public RESTfulResult readExcel(@RequestParam String path) {
  393. File file = new File(path);
  394. return success();
  395. }
  396. }