feat:谱文件上传

This commit is contained in:
nieziyan 2023-08-24 17:07:19 +08:00
parent 24910d27f4
commit 736210780b
9 changed files with 366 additions and 13 deletions

View File

@ -31,7 +31,15 @@ public interface Prompt {
String PARAM_NOT_EMPTY = "Param Cat Not Be Empty!"; String PARAM_NOT_EMPTY = "Param Cat Not Be Empty!";
String EXEC_SUCC = "Task executed successfully!"; String EXEC_SUCC = "Task Executed Successfully!";
String EXEC_Faild = "Task executed Faild!"; String EXEC_Faild = "Task Executed Faild!";
String FILE_TYPE_ERR = "Incorrect File Type!";
String UPLOAD_SUCC = "File Uploaded Successfully!";
String UPLOAD_ERR = "File Upload Failure!";
String FILE_NAME_REPEAT = "File With The Same Name Appears, Stop Uploading!";
} }

View File

@ -56,9 +56,9 @@ public class FTPUtil {
* @return * @return
*/ */
public FTPClient LoginFTP(){ public FTPClient LoginFTP(){
//声明FTP客户端
FTPClient ftp = new FTPClient();
try { try {
//声明FTP客户端
FTPClient ftp = new FTPClient();
//连接 //连接
ftp.connect(host, port); ftp.connect(host, port);
//登录 //登录
@ -71,10 +71,10 @@ public class FTPUtil {
ftp.disconnect(); ftp.disconnect();
return null; return null;
} }
return ftp;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); return null;
} }
return ftp;
} }
public void downloadFTPFile(String localPath, String fileName, HttpServletResponse response) { public void downloadFTPFile(String localPath, String fileName, HttpServletResponse response) {

View File

@ -0,0 +1,23 @@
package org.jeecg.common.util;
import cn.hutool.core.collection.CollUtil;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class PageUtil {
public static <T> List<T> page(int pageNo, int pageSize, List<T> data) {
if (CollUtil.isEmpty(data))
return Collections.emptyList();
// 计算偏移量
int offset = (pageNo - 1) * pageSize;
// 总记录数
int totalSize = data.size();
if (offset >= totalSize)
return Collections.emptyList();
// 分页结果数据
return data.stream().skip(offset).limit(pageSize)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,24 @@
package org.jeecg.modules.base.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.jeecg.common.constant.DateConstant;
import org.jetbrains.annotations.NotNull;
import java.util.Date;
@Data
public class FileDto implements Comparable<FileDto>{
private String name;
private String size;
@JsonFormat(pattern = DateConstant.DATE_TIME,timezone = DateConstant.TIME_ZONE)
private Date updateDate;
@Override
public int compareTo(@NotNull FileDto o) {
return this.name.compareTo(o.getName());
}
}

View File

@ -16,6 +16,7 @@ import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.DictConstant; import org.jeecg.common.constant.DictConstant;
import org.jeecg.common.constant.SymbolConstant; import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.system.vo.DictModel; import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.PageUtil;
import org.jeecg.modules.base.dto.AlarmAnalysisRuleDto; import org.jeecg.modules.base.dto.AlarmAnalysisRuleDto;
import org.jeecg.modules.base.dto.AnalysisLogDto; import org.jeecg.modules.base.dto.AnalysisLogDto;
import org.jeecg.modules.base.dto.NuclideInfo; import org.jeecg.modules.base.dto.NuclideInfo;
@ -110,9 +111,7 @@ public class AlarmAnalysisLogServiceImpl extends ServiceImpl<AlarmAnalysisLogMap
Integer pageNo = analysisLogVo.getPageNo(); Integer pageNo = analysisLogVo.getPageNo();
Integer pageSize = analysisLogVo.getPageSize(); Integer pageSize = analysisLogVo.getPageSize();
int total = result.size(); int total = result.size();
int start = (pageNo - 1) * pageSize; List<AnalysisLogDto> records = PageUtil.page(pageNo, pageSize, result);
int end = Math.min(start + pageSize, total);
List<AnalysisLogDto> records = result.subList(start,end);
Page<AnalysisLogDto> page = new Page<>(pageNo,pageSize,total); Page<AnalysisLogDto> page = new Page<>(pageNo,pageSize,total);
page.setRecords(records); page.setRecords(records);
return Result.OK(page); return Result.OK(page);

View File

@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.*; import org.jeecg.common.constant.*;
import org.jeecg.common.system.vo.DictModel; import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.PageUtil;
import org.jeecg.common.util.RedisStreamUtil; import org.jeecg.common.util.RedisStreamUtil;
import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.base.dto.AlarmAnalysisRuleDto; import org.jeecg.modules.base.dto.AlarmAnalysisRuleDto;
@ -126,10 +127,7 @@ public class AlarmAnalysisRuleServiceImpl extends ServiceImpl<AlarmAnalysisRuleM
} }
// 封装分页 // 封装分页
int total = dtos.size(); int total = dtos.size();
int start = (pageNo - 1) * pageSize; List<AlarmAnalysisRuleDto> records = PageUtil.page(pageNo, pageSize, dtos);
int end = Math.min(start + pageSize, total);
List<AlarmAnalysisRuleDto> records = dtos.subList(start, end);
Page<AlarmAnalysisRuleDto> page = new Page<>(pageNo,pageSize,total); Page<AlarmAnalysisRuleDto> page = new Page<>(pageNo,pageSize,total);
page.setRecords(records); page.setRecords(records);
return Result.OK(page); return Result.OK(page);

View File

@ -0,0 +1,93 @@
package org.jeecg.modules.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.QueryRequest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.Prompt;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.constant.enums.FileTypeEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FTPUtil;
import org.jeecg.modules.base.dto.FileDto;
import org.jeecg.modules.service.ISpectrumFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@RestController
@RequestMapping("spectrumFile")
@Api(value = "谱文件上传管理",tags = "谱文件上传管理")
public class SpectrumFileController {
@Autowired
private ISpectrumFileService spectrumFileService;
@PostMapping("upload")
@ApiOperation(value = "谱文件上传",notes = "谱文件上传")
public Result<?> upload(@RequestParam MultipartFile file,
@RequestParam String choice){
return spectrumFileService.upload(file,choice);
}
@GetMapping("get")
@ApiOperation(value = "用户谱文件列表",notes = "用户谱文件列表")
public Result<?> get(QueryRequest query){
Integer pageNo = query.getPageNo();
Integer pageSize = query.getPageSize();
return spectrumFileService.get(pageNo,pageSize);
}
public static void unzip(File zipFile, String destDir) throws IOException {
byte[] buffer = new byte[1024];
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile))) {
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(destDir + File.separator + fileName);
if (zipEntry.isDirectory()) {
newFile.mkdirs();
} else {
new File(newFile.getParent()).mkdirs();
try (FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zipInputStream.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
}
}
public static void main(String[] args) {
String zipFilePath = "C:\\Users\\a\\Desktop\\2.zip"; // 替换为你的zip文件路径
String destDir = "C:\\Users\\a\\Desktop\\1"; // 替换为你的解压目标目录路径
try {
unzip(new File(zipFilePath), destDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,13 @@
package org.jeecg.modules.service;
import org.jeecg.common.api.QueryRequest;
import org.jeecg.common.api.vo.Result;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
public interface ISpectrumFileService {
Result<?> upload(MultipartFile file,String choice);
Result<?> get(Integer pageNo,Integer pageSize);
}

View File

@ -0,0 +1,195 @@
package org.jeecg.modules.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.Prompt;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.constant.enums.FileTypeEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FTPUtil;
import org.jeecg.common.util.PageUtil;
import org.jeecg.modules.base.dto.FileDto;
import org.jeecg.modules.service.ISpectrumFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@Service
public class SpectrumFileServiceImpl implements ISpectrumFileService {
@Autowired
private FTPUtil ftpUtil;
@Override
public Result<?> upload(MultipartFile file, String choice) {
String filename = file.getOriginalFilename();
boolean isZip = filename.endsWith(FileTypeEnum.zip.getType());
if (!isZip) return Result.error(Prompt.FILE_TYPE_ERR);
LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
String username = user.getUsername();
FTPClient ftpClient = null;
FileOutputStream fos = null;
ZipInputStream zipInputStream = null;
String slash = SymbolConstant.SINGLE_SLASH;
String filePath = "/SpectrumFile" + slash + username;
String tempFilePath = System.getProperty("java.io.tmpdir") + username + slash;
List<String> fileNames = new ArrayList<>();
List<File> fileList = new ArrayList<>();
try{
File tempDir = new File(tempFilePath);
if (!tempDir.exists()) tempDir.mkdir();
zipInputStream = new ZipInputStream(file.getInputStream());
ZipEntry entry;
while (ObjectUtil.isNotNull(entry = zipInputStream.getNextEntry())) {
String fileName = entry.getName();
fileNames.add(fileName);
File oneFile = new File(tempFilePath + fileName);
fos = new FileOutputStream(oneFile);
byte[] bytes = new byte[1024];
int length;
while ((length = zipInputStream.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
}
fileList.add(oneFile);
}
ftpClient = ftpUtil.LoginFTP();
if (ObjectUtil.isNull(ftpClient))
return Result.error(Prompt.FTP_ERR);
// 如果指定目录不存在,直接上传
boolean exist = ftpClient.changeWorkingDirectory(filePath);
if (!exist){
ftpClient.makeDirectory(filePath);
for (File oneFile : fileList) {
String fullFilePath = filePath + slash + oneFile.getName();
FileInputStream local = new FileInputStream(oneFile);
ftpClient.storeFile(fullFilePath,local);
}
return Result.OK(Prompt.UPLOAD_SUCC);
}
// 如果指定目录存在,则获取指定目录下所有文件名
List<String> remoteFileNames = ListUtil.toList(ftpClient.listNames());
Collection<String> crossFileNames = CollUtil.intersection(fileNames, remoteFileNames);
// 没有重名文件,直接全部导入
if (CollUtil.isEmpty(crossFileNames)){
for (File oneFile : fileList) {
String fullFilePath = filePath + slash + oneFile.getName();
FileInputStream local = new FileInputStream(oneFile);
ftpClient.storeFile(fullFilePath,local);
}
return Result.OK(Prompt.UPLOAD_SUCC);
}
// 有重名文件,根据用户选择进行处理
if (StrUtil.equals("stop",choice)){ // 1.停止上传
return Result.error(Prompt.FILE_NAME_REPEAT);
}else if (StrUtil.equals("over",choice)){ // 2.直接覆盖
for (File oneFile : fileList) {
String fullFilePath = filePath + slash + oneFile.getName();
FileInputStream local = new FileInputStream(oneFile);
ftpClient.storeFile(fullFilePath,local);
}
}else if (StrUtil.equals("skip",choice)){ // 3.保留原文件
for (File oneFile : fileList) {
String oneFileName = oneFile.getName();
if (crossFileNames.contains(oneFileName))
continue;
String fullFilePath = filePath + slash + oneFileName;
FileInputStream local = new FileInputStream(oneFile);
ftpClient.storeFile(fullFilePath,local);
}
}else { // 默认不上传
return Result.error(Prompt.FILE_NAME_REPEAT);
}
return Result.OK(Prompt.UPLOAD_SUCC);
} catch (IOException e) {
e.printStackTrace();
return Result.error(Prompt.UPLOAD_ERR);
}finally {
try {
if (ObjectUtil.isNotNull(zipInputStream))
zipInputStream.close();
if (ObjectUtil.isNotNull(fos))
fos.close();
if (ObjectUtil.isNotNull(ftpClient))
if (ftpClient.isConnected()){
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public Result<?> get(Integer pageNo, Integer pageSize) {
LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
String username = user.getUsername();
String slash = SymbolConstant.SINGLE_SLASH;
String filePath = "/SpectrumFile" + slash + username;
FTPClient ftpClient = null;
List<FileDto> fileDtos = new ArrayList<>();
Page<FileDto> page = new Page<>(pageNo, pageSize);
try {
ftpClient = ftpUtil.LoginFTP();
if (ObjectUtil.isNull(ftpClient))
return Result.error(Prompt.FTP_ERR);
boolean exist = ftpClient.changeWorkingDirectory(filePath);
if (!exist) return Result.OK(page);
List<FTPFile> ftpFiles = ListUtil.toList(ftpClient.listFiles());
for (FTPFile ftpFile : ftpFiles) {
String fileName = ftpFile.getName();
Calendar calendar = ftpFile.getTimestamp();
Date updateDate = calendar.getTime();
long size = ftpFile.getSize();
FileDto fileDto = new FileDto();
fileDto.setName(fileName);
fileDto.setUpdateDate(updateDate);
fileDto.setSize(formatSize(size));
fileDtos.add(fileDto);
}
Collections.sort(fileDtos);
List<FileDto> records = PageUtil.page(pageNo, pageSize, fileDtos);
page.setRecords(records).setTotal(fileDtos.size());
return Result.OK(page);
} catch (IOException e) {
e.printStackTrace();
return Result.error(e.getMessage());
}finally {
try {
if (ObjectUtil.isNotNull(ftpClient))
if (ftpClient.isConnected()){
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String formatSize(long size) {
String[] units = {"B", "kB", "MB", "GB", "TB"};
int index = 0;
double fileSize = size;
while (fileSize >= 1024 && index < units.length - 1) {
fileSize /= 1024;
index++;
}
return String.format("%.2f", fileSize) + " " + units[index];
}
}