Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
SSSDNSY
spring-framework
提交
4b6b12bf
S
spring-framework
项目概览
SSSDNSY
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4b6b12bf
编写于
4月 12, 2021
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Bypass root path resolution for "file:" prefix only
Closes gh-26702
上级
ab0e8f06
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
21 addition
and
29 deletion
+21
-29
spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java
...org/springframework/beans/propertyeditors/PathEditor.java
+10
-7
spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java
...pringframework/beans/propertyeditors/PathEditorTests.java
+11
-22
未找到文件。
spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java
浏览文件 @
4b6b12bf
...
...
@@ -26,8 +26,8 @@ import java.nio.file.Paths;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceEditor
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ResourceUtils
;
/**
* Editor for {@code java.nio.file.Path}, to directly populate a Path
...
...
@@ -74,7 +74,7 @@ public class PathEditor extends PropertyEditorSupport {
@Override
public
void
setAsText
(
String
text
)
throws
IllegalArgumentException
{
boolean
nioPathCandidate
=
!
text
.
startsWith
(
Resource
Loader
.
CLASSPATH_URL_PREFIX
);
boolean
nioPathCandidate
=
!
text
.
startsWith
(
Resource
Utils
.
CLASSPATH_URL_PREFIX
);
if
(
nioPathCandidate
&&
!
text
.
startsWith
(
"/"
))
{
try
{
URI
uri
=
new
URI
(
text
);
...
...
@@ -85,9 +85,13 @@ public class PathEditor extends PropertyEditorSupport {
return
;
}
}
catch
(
URISyntaxException
|
FileSystemNotFoundException
ex
)
{
// Not a valid URI (let's try as Spring resource location),
// or a URI scheme not registered for NIO (let's try URL
catch
(
URISyntaxException
ex
)
{
// Not a valid URI; potentially a Windows-style path after
// a file prefix (let's try as Spring resource location)
nioPathCandidate
=
!
text
.
startsWith
(
ResourceUtils
.
FILE_URL_PREFIX
);
}
catch
(
FileSystemNotFoundException
ex
)
{
// URI scheme not registered for NIO (let's try URL
// protocol handlers via Spring's resource mechanism).
}
}
...
...
@@ -97,8 +101,7 @@ public class PathEditor extends PropertyEditorSupport {
if
(
resource
==
null
)
{
setValue
(
null
);
}
else
if
(!
resource
.
isFile
()
&&
!
resource
.
exists
()
&&
nioPathCandidate
)
{
// Prefer getFile().toPath() below for non-existent file handles
else
if
(
nioPathCandidate
&&
!
resource
.
exists
())
{
setValue
(
Paths
.
get
(
text
).
normalize
());
}
else
{
...
...
spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java
浏览文件 @
4b6b12bf
...
...
@@ -39,8 +39,7 @@ public class PathEditorTests {
pathEditor
.
setAsText
(
"classpath:"
+
ClassUtils
.
classPackageAsResourcePath
(
getClass
())
+
"/"
+
ClassUtils
.
getShortName
(
getClass
())
+
".class"
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition
=
value
instanceof
Path
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
assertThat
(
path
.
toFile
().
exists
()).
isTrue
();
}
...
...
@@ -57,11 +56,9 @@ public class PathEditorTests {
PropertyEditor
pathEditor
=
new
PathEditor
();
pathEditor
.
setAsText
(
"file:/no_way_this_file_is_found.doc"
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition1
=
value
instanceof
Path
;
assertThat
(
condition1
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
boolean
condition
=
!
path
.
toFile
().
exists
();
assertThat
(
condition
).
isTrue
();
assertThat
(!
path
.
toFile
().
exists
()).
isTrue
();
}
@Test
...
...
@@ -69,11 +66,9 @@ public class PathEditorTests {
PropertyEditor
pathEditor
=
new
PathEditor
();
pathEditor
.
setAsText
(
"/no_way_this_file_is_found.doc"
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition1
=
value
instanceof
Path
;
assertThat
(
condition1
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
boolean
condition
=
!
path
.
toFile
().
exists
();
assertThat
(
condition
).
isTrue
();
assertThat
(!
path
.
toFile
().
exists
()).
isTrue
();
}
@Test
...
...
@@ -81,11 +76,9 @@ public class PathEditorTests {
PropertyEditor
pathEditor
=
new
PathEditor
();
pathEditor
.
setAsText
(
"C:\\no_way_this_file_is_found.doc"
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition1
=
value
instanceof
Path
;
assertThat
(
condition1
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
boolean
condition
=
!
path
.
toFile
().
exists
();
assertThat
(
condition
).
isTrue
();
assertThat
(!
path
.
toFile
().
exists
()).
isTrue
();
}
@Test
...
...
@@ -93,11 +86,9 @@ public class PathEditorTests {
PropertyEditor
pathEditor
=
new
PathEditor
();
pathEditor
.
setAsText
(
"file://C:\\no_way_this_file_is_found.doc"
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition1
=
value
instanceof
Path
;
assertThat
(
condition1
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
boolean
condition
=
!
path
.
toFile
().
exists
();
assertThat
(
condition
).
isTrue
();
assertThat
(!
path
.
toFile
().
exists
()).
isTrue
();
}
@Test
...
...
@@ -107,8 +98,7 @@ public class PathEditorTests {
ClassUtils
.
getShortName
(
getClass
())
+
".class"
;
pathEditor
.
setAsText
(
fileName
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition
=
value
instanceof
Path
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
File
file
=
path
.
toFile
();
assertThat
(
file
.
exists
()).
isTrue
();
...
...
@@ -126,8 +116,7 @@ public class PathEditorTests {
ClassUtils
.
getShortName
(
getClass
())
+
".clazz"
;
pathEditor
.
setAsText
(
fileName
);
Object
value
=
pathEditor
.
getValue
();
boolean
condition
=
value
instanceof
Path
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
instanceof
Path
).
isTrue
();
Path
path
=
(
Path
)
value
;
File
file
=
path
.
toFile
();
assertThat
(
file
.
exists
()).
isFalse
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录