mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
修复FileNameUtil.cleanInvalid无法去除换行符问题
This commit is contained in:
parent
351f5305d9
commit
e0bb2da87b
@ -3,11 +3,12 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.8.8.M1 (2022-09-15)
|
||||
# 5.8.8.M1 (2022-09-17)
|
||||
|
||||
### 🐣新特性
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复FileNameUtil.cleanInvalid无法去除换行符问题(issue#I5RMZV@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -40,7 +40,7 @@ public class FileNameUtil {
|
||||
/**
|
||||
* Windows下文件名中的无效字符
|
||||
*/
|
||||
private static final Pattern FILE_NAME_INVALID_PATTERN_WIN = Pattern.compile("[\\\\/:*?\"<>|]");
|
||||
private static final Pattern FILE_NAME_INVALID_PATTERN_WIN = Pattern.compile("[\\\\/:*?\"<>|\r\n]");
|
||||
|
||||
/**
|
||||
* 特殊后缀
|
||||
@ -223,18 +223,18 @@ public class FileNameUtil {
|
||||
if (fileName == null) {
|
||||
return null;
|
||||
}
|
||||
int index = fileName.lastIndexOf(StrUtil.DOT);
|
||||
final int index = fileName.lastIndexOf(StrUtil.DOT);
|
||||
if (index == -1) {
|
||||
return StrUtil.EMPTY;
|
||||
} else {
|
||||
// issue#I4W5FS@Gitee
|
||||
int secondToLastIndex = fileName.substring(0, index).lastIndexOf(StrUtil.DOT);
|
||||
String substr = fileName.substring(secondToLastIndex == -1 ? index : secondToLastIndex + 1);
|
||||
final int secondToLastIndex = fileName.substring(0, index).lastIndexOf(StrUtil.DOT);
|
||||
final String substr = fileName.substring(secondToLastIndex == -1 ? index : secondToLastIndex + 1);
|
||||
if (StrUtil.containsAny(substr, SPECIAL_SUFFIX)) {
|
||||
return substr;
|
||||
}
|
||||
|
||||
String ext = fileName.substring(index + 1);
|
||||
final String ext = fileName.substring(index + 1);
|
||||
// 扩展名中不能包含路径相关的符号
|
||||
return StrUtil.containsAny(ext, UNIX_SEPARATOR, WINDOWS_SEPARATOR) ? StrUtil.EMPTY : ext;
|
||||
}
|
||||
|
16
hutool-core/src/test/java/cn/hutool/core/io/file/FileNameUtilTest.java
Executable file
16
hutool-core/src/test/java/cn/hutool/core/io/file/FileNameUtilTest.java
Executable file
@ -0,0 +1,16 @@
|
||||
package cn.hutool.core.io.file;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileNameUtilTest {
|
||||
|
||||
@Test
|
||||
public void cleanInvalidTest(){
|
||||
String name = FileNameUtil.cleanInvalid("1\n2\n");
|
||||
Assert.assertEquals("12", name);
|
||||
|
||||
name = FileNameUtil.cleanInvalid("\r1\r\n2\n");
|
||||
Assert.assertEquals("12", name);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user