mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复FileUtil.checkSlip方法缺陷
This commit is contained in:
parent
11655b0ad2
commit
d53ac71616
@ -3459,25 +3459,13 @@ public class FileUtil extends PathUtil {
|
|||||||
*/
|
*/
|
||||||
public static File checkSlip(File parentFile, File file) throws IllegalArgumentException {
|
public static File checkSlip(File parentFile, File file) throws IllegalArgumentException {
|
||||||
if (null != parentFile && null != file) {
|
if (null != parentFile && null != file) {
|
||||||
if (false == startsWith(parentFile, file)) {
|
if (false == isSub(parentFile, file)) {
|
||||||
throw new IllegalArgumentException("New file is outside of the parent dir: " + file.getName());
|
throw new IllegalArgumentException("New file is outside of the parent dir: " + file.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查父文件是否为文件真正的父目录
|
|
||||||
*
|
|
||||||
* @param parentFile 父目录
|
|
||||||
* @param file 文件
|
|
||||||
* @return 是否为文件真正的父目录
|
|
||||||
*/
|
|
||||||
public static boolean startsWith(final File parentFile, final File file) {
|
|
||||||
return PathUtil.toAbsNormal(parentFile.toPath())
|
|
||||||
.startsWith(PathUtil.toAbsNormal(file.toPath()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据文件扩展名获得MimeType
|
* 根据文件扩展名获得MimeType
|
||||||
*
|
*
|
||||||
|
@ -20,8 +20,14 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class FileUtilTest {
|
public class FileUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fileTest1() {
|
||||||
|
final File file = FileUtil.file("d:/aaa", "bbb");
|
||||||
|
Assert.assertNotNull(file);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void fileTest() {
|
public void fileTest2() {
|
||||||
final File file = FileUtil.file("d:/aaa", "bbb");
|
final File file = FileUtil.file("d:/aaa", "bbb");
|
||||||
Assert.assertNotNull(file);
|
Assert.assertNotNull(file);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user