Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
9be0cf21
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
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,体验更适合开发者的 AI 搜索 >>
提交
9be0cf21
编写于
9月 25, 2014
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support writing multipart non-ASCII file names
Issue: SPR-12108
上级
6cbe1433
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
2 deletion
+48
-2
build.gradle
build.gradle
+1
-0
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
...ingframework/http/converter/FormHttpMessageConverter.java
+27
-1
spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
...amework/http/converter/FormHttpMessageConverterTests.java
+20
-1
未找到文件。
build.gradle
浏览文件 @
9be0cf21
...
...
@@ -684,6 +684,7 @@ project("spring-web") {
optional
(
"log4j:log4j:1.2.17"
)
optional
(
"com.googlecode.protobuf-java-format:protobuf-java-format:1.2"
)
optional
(
"com.google.protobuf:protobuf-java:${protobufVersion}"
)
optional
(
"javax.mail:javax.mail-api:1.5.2"
)
testCompile
(
project
(
":spring-context-support"
))
// for JafMediaTypeFactory
testCompile
(
"xmlunit:xmlunit:1.5"
)
testCompile
(
"org.slf4j:slf4j-jcl:${slf4jVersion}"
)
...
...
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
浏览文件 @
9be0cf21
...
...
@@ -41,6 +41,8 @@ import org.springframework.util.MultiValueMap;
import
org.springframework.util.StreamUtils
;
import
org.springframework.util.StringUtils
;
import
javax.mail.internet.MimeUtility
;
/**
* Implementation of {@link HttpMessageConverter} to read and write 'normal' HTML
* forms and also to write (but not read) multipart data (e.g. file uploads).
...
...
@@ -92,6 +94,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
private
Charset
charset
=
Charset
.
forName
(
"UTF-8"
);
private
Charset
multipartCharset
;
private
List
<
MediaType
>
supportedMediaTypes
=
new
ArrayList
<
MediaType
>();
private
List
<
HttpMessageConverter
<?>>
partConverters
=
new
ArrayList
<
HttpMessageConverter
<?>>();
...
...
@@ -118,6 +122,18 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
this
.
charset
=
charset
;
}
/**
* Set the character set to use when writing multipart data to encode file
* names. Encoding is based on the encoded-word syntax defined in RFC 2047
* and relies on the MimeUtility class from "javax.mail-api".
* <p>If not set file names will be encoded as US-ASCII.
* @param multipartCharset the charset to use
* @see <a href="http://en.wikipedia.org/wiki/MIME#Encoded-Word">Encoded-Word</a>
*/
public
void
setMultipartCharset
(
Charset
multipartCharset
)
{
this
.
multipartCharset
=
multipartCharset
;
}
/**
* Set the list of {@link MediaType} objects supported by this converter.
*/
...
...
@@ -374,7 +390,17 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
protected
String
getFilename
(
Object
part
)
{
if
(
part
instanceof
Resource
)
{
Resource
resource
=
(
Resource
)
part
;
return
resource
.
getFilename
();
String
filename
=
resource
.
getFilename
();
if
(
multipartCharset
!=
null
)
{
try
{
filename
=
MimeUtility
.
encodeText
(
filename
,
multipartCharset
.
name
(),
null
);
}
catch
(
UnsupportedEncodingException
e
)
{
// should not happen
throw
new
IllegalStateException
(
e
);
}
}
return
filename
;
}
else
{
return
null
;
...
...
spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
浏览文件 @
9be0cf21
...
...
@@ -116,6 +116,17 @@ public class FormHttpMessageConverterTests {
Resource
logo
=
new
ClassPathResource
(
"/org/springframework/http/converter/logo.jpg"
);
parts
.
add
(
"logo"
,
logo
);
// SPR-12108
Resource
utf8
=
new
ClassPathResource
(
"/org/springframework/http/converter/logo.jpg"
)
{
@Override
public
String
getFilename
()
{
return
"Hall\u00F6le.jpg"
;
}
};
parts
.
add
(
"utf8"
,
utf8
);
Source
xml
=
new
StreamSource
(
new
StringReader
(
"<root><child/></root>"
));
HttpHeaders
entityHeaders
=
new
HttpHeaders
();
entityHeaders
.
setContentType
(
MediaType
.
TEXT_XML
);
...
...
@@ -123,6 +134,7 @@ public class FormHttpMessageConverterTests {
parts
.
add
(
"xml"
,
entity
);
MockHttpOutputMessage
outputMessage
=
new
MockHttpOutputMessage
();
converter
.
setMultipartCharset
(
Charset
.
forName
(
"UTF-8"
));
converter
.
write
(
parts
,
MediaType
.
MULTIPART_FORM_DATA
,
outputMessage
);
final
MediaType
contentType
=
outputMessage
.
getHeaders
().
getContentType
();
...
...
@@ -132,7 +144,7 @@ public class FormHttpMessageConverterTests {
FileItemFactory
fileItemFactory
=
new
DiskFileItemFactory
();
FileUpload
fileUpload
=
new
FileUpload
(
fileItemFactory
);
List
<
FileItem
>
items
=
fileUpload
.
parseRequest
(
new
MockHttpOutputMessageRequestContext
(
outputMessage
));
assertEquals
(
5
,
items
.
size
());
assertEquals
(
6
,
items
.
size
());
FileItem
item
=
items
.
get
(
0
);
assertTrue
(
item
.
isFormField
());
assertEquals
(
"name 1"
,
item
.
getFieldName
());
...
...
@@ -156,6 +168,13 @@ public class FormHttpMessageConverterTests {
assertEquals
(
logo
.
getFile
().
length
(),
item
.
getSize
());
item
=
items
.
get
(
4
);
assertFalse
(
item
.
isFormField
());
assertEquals
(
"utf8"
,
item
.
getFieldName
());
assertEquals
(
"Hall\u00F6le.jpg"
,
item
.
getName
());
assertEquals
(
"image/jpeg"
,
item
.
getContentType
());
assertEquals
(
logo
.
getFile
().
length
(),
item
.
getSize
());
item
=
items
.
get
(
5
);
assertEquals
(
"xml"
,
item
.
getFieldName
());
assertEquals
(
"text/xml"
,
item
.
getContentType
());
verify
(
outputMessage
.
getBody
(),
never
()).
close
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录