| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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<Object, MultipartFile> FileList, MultipartRequest request) throws Exception {
- List<MultipartFile> 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<Map<String,Object>> mapList = systemFileMapper.getFileInfo(uuidList);
- List<Map<String,Object>> resultList = new ArrayList<>();
- for (Map<String,Object> 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<String,Object> result = ftpFileUtil.downloadFileNew(filename, filepath);
- resultList.add(result);
- } catch (IOException e) {
- e.getMessage();
- continue;
- }
- }
- return success(resultList);
- }
- }
|