我从 Spring 4.3.16 升级到 Spring 5.0.7。尝试使用 restTemplate 上传文件时,我开始收到“ 400 - Bad Request ”。在弄乱了我注意到的唯一行为差异之后,我注意到从交换调用中删除 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”。[file "/etc/modsecurity/modsecurity.conf"] [line "54"] [id "200001"] [msg "Failed to parse request body."] [data "Multipart: Invalid boundary in CT (characters)." ] [严重性 "CRITICAL"] [主机名 ""] [uri "/restless/documentUpload/multipleUpload"] [unique_id "W09WhX8AAAEAAAAUhs7IAAABI"]