我从Spring 4.3.16升级到Spring 5.0.7。当尝试使用restTemplate上传文件时,我开始收到“ 400-错误的请求”。弄乱了唯一的行为差异后,我注意到从交换呼叫中删除了requestEntity,错误(错误的请求)消失了,但是失败了,因为不存在要上传的文件。
任何帮助将不胜感激!
客户:
public <T> ResponseEntity<T> uploadMultipartFile(String requestParamName, byte[] byteArray, String fileName,
Class<T> responseType) {
ByteArrayResource byteArrayAsResource = new ByteArrayResource(byteArray) {
@Override
public String getFilename() {
return fileName;
}
};
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add(requestParamName, byteArrayAsResource);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
return getRestTemplate().exchange(buildUriWithVariables(new HashMap<>()), HttpMethod.POST, requestEntity, responseType);
}
和控制器:
@RequestMapping("/multipleUpload")
public ModelAndView multipleUpload(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "files", required = false) MultipartFile[] files,
@ModelAttribute("document") Document document,
BindingResult result) {}
Apache似乎在抱怨请求正文:
[2018年7月18日星期三11:02:29.705758] [:错误] [pid 1313:tid 140177850750720] [客户端] ModSecurity:访问被拒绝 代码400(第2阶段)。必须将“ eq 0”与“ REQBODY_ERROR”进行匹配。 [文件“ /etc/modsecurity/modsecurity.conf”] [第“ 54行] [id” 200001“] [msg“无法解析请求正文。”] [数据“多部分:无效 C-T中的边界(字符)。“] [严重性“ CRITICAL”] [主机名 “”] [uri“ / restless / documentUpload / multipleUpload”] [unique_id“ W09WhX8AAAEAAAUhs7IAAABI”]