当前位置:网站首页>上传文件列表(文件名重复加括号标识)
上传文件列表(文件名重复加括号标识)
2022-06-28 18:13:00 【三级码农666】
/**
* 文件上传
*
* @param files
*/
@Override
public void uploadFiles(MultipartFile[] files, String url) {
Assert.notNull(files, "上传文件不能为空");
for (int i = 0; i < files.length; i++) {
// 文件全路径
List<File> fileList = Arrays.asList(new File(url).listFiles());
int finalI1 = i;
// 查找当前文件是否存在此文件名
// 上传文件截取
String[] sources = files[finalI1].getOriginalFilename().split("\\.");
int length = sources[0].length();
// 查找除去后缀重复的文件名list
List<File> list = fileList.stream().filter(d -> d.getName().contains(sources[0])).collect(Collectors.toList());
// 从重复list中过滤出带括号的
List<File> filterFiles = list.stream().filter(b -> b.getName().contains(").")).collect(Collectors.toList());
// 只有一个重复,且是第一次下载
List<File> noFilterFiles = list.stream().filter(b -> !b.getName().contains(").")).collect(Collectors.toList());
// 将重复list的后缀标号截取成整数list
List<Integer> integerList = filterFiles.stream().map(d -> Integer.parseInt(d.getName().substring(length + 1, d.getName().lastIndexOf(".") - 1))).sorted().collect(Collectors.toList());
if (!ObjectUtils.isEmpty(noFilterFiles)) {
integerList.add(0);
}
// 上传文件下标新增,规则操作
List<Integer> integers = integerList.stream().sorted().collect(Collectors.toList());
Integer maxNum = integers.get(integerList.size() - 1);
Integer index = 1;
for (int j = 0; j <= maxNum; j++) {
if (integers.get(j) != j) {
index = j;
break;
}
index = integers.get(j).equals(maxNum) ? maxNum + 1 : 1;
}
// 不存在重复文件、或者下标缺0的都不加括号和编号
String countStr = list.size() <= 0 || index == 0 ? "" : "(" + index + ")";
// 拼接文件名和编号
File file = new File(url + File.separator +
files[i].getOriginalFilename().split("\\.")[0] + countStr + "." + files[i].getOriginalFilename().split("\\.")[1]);
try {
Files.createParentDirs(file.getParentFile());
files[i].transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
- HackTheBox-baby CachedView
- MySQL十种锁,一篇文章带你全解析
- 华为云AOM发布2.0版本,3大特性亮相
- 图形系统——2.view绘制
- 抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱
- 如何从RHEL 8升级到RHEL 9
- How to use the current conversion function in SAP CDs view
- EasyCVR新建用户后,视频调阅页面不能点击的问题修复
- Applet graduation design based on wechat conference room reservation applet graduation design opening report function reference
- Sharing-JDBC分布式事务之Seata实现
猜你喜欢

moco挡板制作及运行成功

A preliminary study of IO model

Graphic system - 1 Layout loading

Shanghai Pudong Development Bank Software Test interview real question

Applet graduation project is based on wechat property maintenance application property applet graduation project opening report function reference

Mycat+分库分表

PCB线路板布局和布线都有哪些设计要求?

Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference

用户网络模型与QoE

konva系列教程3:自定义图形
随机推荐
February 15, 2022 learning summary
PHP使用栈解决迷宫问题
Apifox 介绍
sqrt()函数的详解和用法「建议收藏」
Redis6 notes 04 master-slave replication, cluster, application problems, new redis6 features
Small program graduation design based on wechat real estate intermediary house viewing appointment small program graduation design opening report function reference
MySQL十种锁,一篇文章带你全解析
select/poll/epoll
亿信华辰:地产企业数字化转型想要把握时代机遇
BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
kubernetes可视化界面dashboard
抓包整理外篇fiddler————了解工具栏[一]
Small program graduation project based on wechat examination small program graduation project opening report function reference
Mycat+ sub database and sub table
WiFi安全漏洞KRACK深度解读
[cloud resident co creation] shengteng heterogeneous computing architecture cann helps release the surging computing power of hardware
PMP怎么补考?补考费用是多少?
konva系列教程3:自定义图形
SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案
用户网络模型与QoE