修复FileNameUtil.cleanInvalid无法去除换行符问题

This commit is contained in:
Looly 2022-09-17 22:27:51 +08:00
parent 351f5305d9
commit e0bb2da87b
3 changed files with 23 additions and 6 deletions

View File

@ -3,11 +3,12 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.8.M1 (2022-09-15) # 5.8.8.M1 (2022-09-17)
### 🐣新特性 ### 🐣新特性
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复FileNameUtil.cleanInvalid无法去除换行符问题issue#I5RMZV@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -40,7 +40,7 @@ public class FileNameUtil {
/** /**
* Windows下文件名中的无效字符 * 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) { if (fileName == null) {
return null; return null;
} }
int index = fileName.lastIndexOf(StrUtil.DOT); final int index = fileName.lastIndexOf(StrUtil.DOT);
if (index == -1) { if (index == -1) {
return StrUtil.EMPTY; return StrUtil.EMPTY;
} else { } else {
// issue#I4W5FS@Gitee // issue#I4W5FS@Gitee
int secondToLastIndex = fileName.substring(0, index).lastIndexOf(StrUtil.DOT); final int secondToLastIndex = fileName.substring(0, index).lastIndexOf(StrUtil.DOT);
String substr = fileName.substring(secondToLastIndex == -1 ? index : secondToLastIndex + 1); final String substr = fileName.substring(secondToLastIndex == -1 ? index : secondToLastIndex + 1);
if (StrUtil.containsAny(substr, SPECIAL_SUFFIX)) { if (StrUtil.containsAny(substr, SPECIAL_SUFFIX)) {
return substr; 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; return StrUtil.containsAny(ext, UNIX_SEPARATOR, WINDOWS_SEPARATOR) ? StrUtil.EMPTY : ext;
} }

View 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);
}
}