Android 多部分请求spring不绑定文件数据
我写的一个控制器有问题,它是为了把照片上传到服务器 控制器Android 多部分请求spring不绑定文件数据,android,spring,spring-mvc,multipart,okhttp,Android,Spring,Spring Mvc,Multipart,Okhttp,我写的一个控制器有问题,它是为了把照片上传到服务器 控制器 @RequestMapping(value = "photos", method = RequestMethod.POST) @ResponseBody public Response uploadPhoto(@RequestPart PhotoMetaData data, @RequestParam String localName,
@RequestMapping(value = "photos", method = RequestMethod.POST)
@ResponseBody
public Response uploadPhoto(@RequestPart PhotoMetaData data,
@RequestParam String localName,
@RequestPart(required = false) MultipartFile file,
HttpServletRequest request) {
log.info("@uploadPhoto > ip of request: " + request.getRemoteAddr() + ", metaData: " + data);
return photosService.storePhoto(data, file, localName);
}
问题是文件
为空,但在检查请求
参数时,请求显然有3个多部分参数,每个参数也有其假定的contentType,但文件是一个长字符串
Android应用程序正在调用此代码。我使用OkHttp来构建多部分请求。代码:
MediaType jsonMediaType = MediaType.parse("application/json");
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(Headers.of("Content-Disposition", "form-data; name=\"data\""),
RequestBody.create(jsonMediaType, photoMetaDataStr))
.addPart(Headers.of("Content-Disposition", "form-data; name=\"localName\""),
RequestBody.create(MediaType.parse("text/plain"), localName.getPath()))
.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\""),
RequestBody.create(MediaType.parse("image/jpeg"), new File(localName.getPath())))
.build();
Request request = new Request.Builder().url(url).post(requestBody).build();
final Response response = client.newCall(request)
.execute();
------编辑------------
相关bean:
@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
----编辑2-----
在更改控制器签名以便需要文件后,我得到一个异常:
----编辑3------
经过大量测试后,我注意到问题可能是我使用okHttp向服务器发送多部分请求的方式。使用Postman客户端,呼叫成功
error with request org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest@3d854606
org.springframework.web.multipart.support
.MissingServletRequestPartException: Required request part 'file' is not present.
谢谢你的时间和帮助
Roy我可以通过在请求中添加内容传输编码头来解决这个问题
RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
.addPart(Headers.of("Content-Disposition", "form-data; name=\"data\""),
RequestBody.create(jsonMediaType, GsonInstance.getInstance()
.toJson(photoMetaData)))
.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\"; filename=\"localName\"", "Content-Transfer-Encoding", "binary"),
RequestBody.create(MediaType.parse("image/jpeg"), new File(localName.getPath())))
.build();
我不知道这有什么关系。根据我的理解,当contentType为image时,默认的传输编码是二进制的。也许这是okHttp的一个小错误?请尝试使用
@RequestPart
而不是@RequestParam
获取多部分文件
参数。@M.Deinum感谢您的回复。已经尝试了所有可能的组合,包括将@RequestParam
更改为@RequestPart
。不起作用如果不起作用你的配置有缺陷。一切都是字符串
或String[]
这一事实并不奇怪,因为web就是这样工作的,参数随后会被转换。但是,它应该是@RequestPart
而不是@RequestParam
从字符串
到请求类型的转换在这两者之间处理方式截然不同。请确保使用调试信息进行编译,否则请尝试@RequestPart(value=“file”),required=false)
进行调试,您可以尝试将required
设置为true(并查看正在抛出的错误)。@M.Deinum我还猜测我的配置存在缺陷,但无法真正理解什么,只有一个bean(据我所知)这需要为多部分请求定义,我处理了它。我按照你的建议做了,并将所需部分添加到了文件中,但例外情况是…不是很大的帮助/-: