作者:WingKeii- | 来源:互联网 | 2023-07-30 12:07
使用Http-Client 4.x的客户端代码:
multipartentityBuilder builder = multipartentityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file,ContentType.MULTIPART_FORM_DATA);
builder.addPart("files",fileBody);
builder.addPart("data",new StringBody(data,ContentType.APPLICATION_JSON));
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
requestObject = httpPost;
if (requestObject != null) {
setHeaders(requestObject);
try {
HttpResponse respOnse= null;
_log.info("[invokeHttpMethod] request.URI=" + requestObject.geturi().toASCIIString() + ",Method=" + requestObject.getMethod() + ",Authorization="
+ requestObject.getFirstHeader(Iconstant.AUTHORIZATION)+ ",Content Type: " +requestObject.getFirstHeader(Iconstant.CONTENT_TYPE));
respOnse= httpClient.execute(requestObject);
服务器代码:
@RequestMapping(值=“ / notification / file”,方法= RequestMethod.POST,消耗= MediaType.MULTIPART_FORM_DATA_VALUE,标头=“ accept = application / xml,application / json”)
公共@ResponseBody ResponseEntity processnotificationFile(
@RequestHeader(value = "Authorization",required = true) String authorization,@RequestBody(required = true) NotificationRequest notificationRequest,@RequestParam("files") MultipartFile multiPart[]) {
_log.info("[processnotificationFile] Started.."+ multiPart);
ResponseEntity respOnseEntity= null;
try {
//notificationEngineservice.processnotification(notificationRequest);
} catch (Exception e) {
e.printStackTrace();
}
respOnseEntity= new ResponseEntity(HttpStatus.OK);
return responseEntity;
}
错误:
HTTP状态415-
类型状态报告
消息
描述服务器拒绝了此请求,因为请求实体的格式不受请求方法的请求资源所支持。
Apache Tomcat / 7.0.42
即使我在服务器控制器剩余端点中添加了多部分功能,但仍然遇到相同的问题