我有这样一种方法:
@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中,我有这个:
# fileupload spring.servlet.multipart.max-file-size=1024MB spring.servlet.multipart.max-request-size=1024MB spring.servlet.multipart.enabled=true
当我尝试上传1.2 MB的文件时,我得到413请求实体太大
我究竟做错了什么?
编辑:
这是调用API的React代码:
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), }, ] ); } };
有趣的是,如果媒体处于某个阈值以下,该阈值小于我在属性文件中设置的阈值,那么我可以成功进行此调用
在方法的参数列表中更改@RequestPart(required = false)为@RequestParam("file", required = false)before 。MultipartFile media``createPost
@RequestPart(required = false)
@RequestParam("file", required = false)
MultipartFile media``createPost
我怀疑不是type从中提取的,而是类似的东西。const { uri, type } = imageURI;``multipart/form- data``image/jpeg
type
const { uri, type } = imageURI;``multipart/form- data``image/jpeg
尽管您的Headers部分包含'Content-Type': 'multipart/form- data',但零件类型可能不对应,并且@RequestPart无法找到HttpMessageConverter用于处理较大的多零件文件的零件。
'Content-Type': 'multipart/form- data'
@RequestPart
HttpMessageConverter
@RequestParam注释也可以用于multipart/form- data,并且在您看来,它可以找到合适的注释Converter。
@RequestParam
multipart/form- data
Converter
您可以使用Fiddler之类的工具来查看来自API调用的实际数据,以验证我对零件类型的怀疑。另外,您可以在createPost方法(MultipartHttpServletRequest request)中添加另一个参数,以直接从Java代码中访问各个部分的标题。
createPost
MultipartHttpServletRequest request