我在编写控制器时出现问题,以便将照片上传到服务器.
控制器
@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);
}
问题是文件是null但是在检查请求参数时,请求显然有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
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.
谢谢你的时间和帮助
罗伊
解决方法:
我只能通过向请求添加Content-Transfer-Encoding标头来解决问题.
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的一个小错误?