Spring Boot上载分段413请求实体太大 [英] Spring Boot Upload Multipart 413 Request Entity Too Large
问题描述
我有这样一种方法:
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
@PreAuthorize("@securityService.isAllowedAccessByCurrentUser(#resource?.userId)")
public Post createPost(@RequestPart(required = false) @Valid final MultipartFile media,
@RequestPart(required = true) @Valid PostUploadDto resource, final UriComponentsBuilder uriBuilder,
final HttpServletResponse response) {
return service.create(resource, media);
}
在application.properties中,我有这个:
In application.properties I have this:
# fileupload
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
spring.servlet.multipart.enabled=true
当我尝试上传1.2 MB的文件时,我收到413个请求实体太大
When I try and upload a 1.2 MB file, I get 413 Request Entity Too Large
我在做什么错了?
这是调用API的React代码:
Here is the React code that calls the API:
export const createNewPost = async (caption, userId, tokenData, imageURI, gameId) => {
const resource = {
userId,
caption,
hashtags: allTagsFrom(caption),
mentions: allMentionsFrom(caption),
gameId,
createdOn: moment()
.utc()
.format('YYYY-MM-DD HH:mm:ss'),
};
if (imageURI) {
const { uri, type } = imageURI;
const data = [
{
name: 'resource',
type: 'application/json',
data: JSON.stringify(resource),
},
{
name: 'media',
type,
filename: 'media.jpg',
data: RNFetchBlob.wrap(uri.substring(7)),
},
];
return RNFetchBlob.fetch(
'POST',
`${serverUrl}/post`,
{
Authorization: `Bearer ${tokenData.access_token}`,
'Content-Type': 'multipart/form-data',
Accept: 'application/json',
},
data
);
} else {
return RNFetchBlob.fetch(
'POST',
`${serverUrl}/post`,
{
Authorization: `Bearer ${tokenData.access_token}`,
'Content-Type': 'multipart/form-data',
Accept: 'application/json',
},
[
{
name: 'resource',
type: 'application/json',
data: JSON.stringify(resource),
},
]
);
}
};
有趣的是,如果媒体处于某个阈值以下,该阈值小于我在属性文件中设置的阈值,则我可以成功进行此调用
It's interesting to note that I can succesfully make this call if the media is under a certain threshold, which is smaller than the one i set in the properties file
推荐答案
在createPost
方法的参数列表中,在MultipartFile media
之前将@RequestPart(required = false)
更改为@RequestParam("file", required = false)
.
Change @RequestPart(required = false)
to @RequestParam("file", required = false)
before MultipartFile media
within your createPost
method's argument list.
我怀疑从const { uri, type } = imageURI;
提取的type
不是multipart/form-data
,而是类似image/jpeg
的东西.
I suspect that the type
extracted from const { uri, type } = imageURI;
is not multipart/form-data
, but instead is something like image/jpeg
.
尽管您的Headers部分包含'Content-Type': 'multipart/form-data'
,但零件类型可能不对应,并且
Despite the fact that your Headers section contains 'Content-Type': 'multipart/form-data'
, the part type likely does not correspond, and @RequestPart
is unable to find an HttpMessageConverter
to deal with the larger multi-part files.
@RequestParam
注释也可以用于multipart/form-data
,在您的情况下,它似乎能够找到合适的Converter
.
The @RequestParam
annotation can also be used for multipart/form-data
, and in your case it appears to be able to find an appropriate Converter
.
您可以使用Fiddler之类的工具来查看来自API调用的实际数据,以验证我对零件类型的怀疑.或者,您可以在createPost
方法中添加另一个参数(
You could use some tool like Fiddler to review the actual data coming from your API call in order to verify my suspicions about the part type. Alternatively, you could add another argument to your createPost
method (MultipartHttpServletRequest request
) to gain access to the headers of the individual parts directly from within your Java code.
这篇关于Spring Boot上载分段413请求实体太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!