package com.steerinfo.dil.controller; import com.google.common.collect.Iterators; import com.steerinfo.dil.mapper.SystemFileMapper; import com.steerinfo.dil.util.FtpFileUtil; import com.steerinfo.dil.util.IDutils; import com.steerinfo.framework.controller.BaseRESTfulController; import com.steerinfo.framework.controller.RESTfulResult; import com.steerinfo.framework.service.pagehelper.PageList; import com.steerinfo.framework.utils.collection.ListUtils; import com.steerinfo.dil.model.SystemFile; import com.steerinfo.dil.service.ISystemFileService; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartRequest; import javax.annotation.Resource; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.*; import java.math.BigDecimal; /** * SystemFile RESTful接口: * * @author generator * @version 1.0-SNAPSHORT 2023-10-30 09:19 * 类描述 * 修订历史: * 日期:2023-10-30 * 作者:generator * 参考: * 描述:SystemFile RESTful接口 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved. * @see null */ @RestController @RequestMapping("/${api.version}/systemfiles") public class SystemFileController extends BaseRESTfulController { @Autowired ISystemFileService systemFileService; @Resource SystemFileMapper systemFileMapper; @Autowired private FtpFileUtil ftpFileUtil; //同一个信息包含多个上传文件信息 @PostMapping("/insertFile") public RESTfulResult insertFile(String fileuuid, @ModelAttribute MultipartFile[] file) { String filenames = ""; for (int i = 0; i < file.length; i++) { filenames += file[i].getOriginalFilename() + ";"; } String filesid = ""; if (file != null) { for (int i = 0; i < file.length; i++) { try { //获取系统时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd"); //获取文件名 String oldName = file[i].getOriginalFilename(); //取当前时间的长整形值包含毫秒 String newName = IDutils.getImageName(); //重新命名文件 newName = newName + oldName.substring(oldName.lastIndexOf(".")); String filePath = simpleDateFormat.format(new Date()); //获取输入流 InputStream inputStream = file[i].getInputStream(); boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false); inputStream.close(); if (result) { SystemFile uploadFile = new SystemFile(); uploadFile.setFilename(oldName); uploadFile.setFilepath(filePath + "/" + newName); uploadFile.setId(fileuuid); SystemFile modela = systemFileService.add(uploadFile); if (modela != null) { filesid += "," + modela.getId(); } } else { return failed(null, "上传文件失败"); } } catch (Exception e) { e.getMessage(); } } return success(filesid); } else { return failed(); } } @PostMapping("/insertFiles2") public RESTfulResult insertFiles2(String fileuuid, @ModelAttribute MultipartFile[] file) { String filenames = ""; for (int i = 0; i < file.length; i++) { filenames += file[i].getOriginalFilename() + ";"; } String filesid = ""; String[] fileuuids = fileuuid.split(","); if (file != null) { for (int i = 0; i < file.length; i++) { try { //获取系统时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd"); //获取文件名 String oldName = file[i].getOriginalFilename(); //取当前时间的长整形值包含毫秒 String newName = IDutils.getImageName(); //重新命名文件 newName = newName + oldName.substring(oldName.lastIndexOf(".")); String filePath = simpleDateFormat.format(new Date()); //获取输入流 InputStream inputStream = file[i].getInputStream(); boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false); inputStream.close(); if (result) { SystemFile uploadFile = new SystemFile(); uploadFile.setFilename(oldName); uploadFile.setFilepath(filePath + "/" + newName); uploadFile.setId(fileuuids[i]); SystemFile modela = systemFileService.add(uploadFile); if (modela != null) { filesid += "," + modela.getId(); } } else { return failed(null, "上传文件失败"); } } catch (Exception e) { e.getMessage(); } } return success(filesid); } else { return failed(); } } @PostMapping("/insertFiles") public RESTfulResult insertFiles(String[] fileUuids, String[] fileNames, String[] fileTypes, Map FileList, MultipartRequest request) throws Exception { List files = new ArrayList<>(); int index=-1; for(int j = 0 ; j < fileTypes.length ; j ++ ) { for(int i=0;i<21;i++) { MultipartFile file = request.getFile("file" +j +"" + i); if (file != null) { files.add(file); if (index < 0) { index = i; } } } } if(files.size() == 0) { return failed("传输失败"); } for (int i = 0; i < fileUuids.length; i++) { //获取系统时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd"); //获取文件名 String oldName = fileNames[i].toString(); //取当前时间的长整形值包含毫秒 String newName = IDutils.getImageName(); //重新命名文件 String filePath = simpleDateFormat.format(new Date()) + "/" + fileTypes[i].toString(); //获取输入流 String fileTypeName = files.get(i).getOriginalFilename(); fileTypeName = fileTypeName.substring(fileTypeName.lastIndexOf(".")); newName = oldName + "(" + newName + ")" + fileTypeName; InputStream inputStream = files.get(i).getInputStream(); boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false); inputStream.close(); if (result) { SystemFile uploadFile = new SystemFile(); uploadFile.setFilename(oldName); uploadFile.setFilepath(filePath + "/" + newName); uploadFile.setId(fileUuids[i]); SystemFile modela = systemFileService.add(uploadFile); if (modela != null) { fileUuids[i] += "," + modela.getId(); } } else { return failed(null, "上传文件失败"); } } return success(); } @PostMapping("/previewfile") public RESTfulResult previewfile(@RequestBody HashMap parmas) { String filename = parmas.get("filename").toString(); String filepath = parmas.get("filepath").toString(); if (filename == null || filename.isEmpty()) { return failed("该图片不存在!"); } if (filepath == null || filepath.isEmpty()) { return failed("该图片地址不存在!"); } try { String result = ftpFileUtil.downloadFile(filename, filepath); return success(result); } catch (IOException e) { e.getMessage(); return failed(); } } @PostMapping("/previewfile2") public RESTfulResult previewfile2(@RequestBody HashMap parmas) { SystemFile value = systemFileMapper.selectByPrimaryKey(parmas.get("id").toString()); String fileName = value.getFilename(); String filepath = value.getFilepath(); if (fileName == null || fileName.isEmpty()) { return failed("该图片不存在!"); } if (filepath == null || filepath.isEmpty()) { return failed("该图片地址不存在!"); } try { String result = ftpFileUtil.downloadFile(fileName, filepath); return success(result); } catch (IOException e) { e.getMessage(); return failed(); } } @PostMapping("/updateFile") public RESTfulResult updateFile(String alternateFields1, @ModelAttribute MultipartFile[] file) { systemFileService.delete(alternateFields1); String filenames = ""; for (int i = 0; i < file.length; i++) { filenames += file[i].getOriginalFilename() + ";"; } String filesid = ""; if (file != null) { for (int i = 0; i < file.length; i++) { try { //获取系统时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd"); //获取文件名 String oldName = file[i].getOriginalFilename(); //取当前时间的长整形值包含毫秒 String newName = IDutils.getImageName(); //重新命名文件 newName = newName + oldName.substring(oldName.lastIndexOf(".")); String filePath = simpleDateFormat.format(new Date()); //获取输入流 InputStream inputStream = file[i].getInputStream(); boolean result = ftpFileUtil.uploadToFtp(inputStream, filePath, newName, false); inputStream.close(); if (result) { SystemFile uploadFile = new SystemFile(); uploadFile.setFilename(oldName); uploadFile.setFilepath(filePath + "/" + newName); uploadFile.setId(alternateFields1); SystemFile modela = systemFileService.add(uploadFile); if (modela != null) { filesid += "," + modela.getId(); } } else { return failed(null, "上传文件失败"); } } catch (Exception e) { e.getMessage(); } } return success(filesid); } else { return failed(); } } @PostMapping("/previewfileList") public RESTfulResult previewfileList(@RequestBody HashMap map) { String uuid = map.get("uuid").toString(); String[] uuidList = uuid.split(";"); //遍历数组,去找文件名称和路径 List> mapList = systemFileMapper.getFileInfo(uuidList); List> resultList = new ArrayList<>(); for (Map parmas : mapList) { String filename = parmas.get("FILENAME").toString(); String filepath = parmas.get("FILEPATH").toString(); if (filename == null ||filename.isEmpty()) { return failed("该图片不存在!"); } if (filepath == null ||filepath.isEmpty()) { return failed("该图片地址不存在!"); } try { Map result = ftpFileUtil.downloadFileNew(filename, filepath); resultList.add(result); } catch (IOException e) { e.getMessage(); continue; } } return success(resultList); } }