在开始我的问题之前,英语不准确,因为我是韩国人。
我使用 Spring Boot 1.5.14。
我正在使用 FormData 实现文件上传,并发生 400 错误。
1.Javascript
var formData = new FormData();
formData.append('autoSelect', 'autoSelect');
formData.append('file', fileObj);
$.ajax({
url: '/api/portfolios/' + pofolNo + '/main-image',
type: 'PUT',
enctype: 'multipart/form-data',
processData: false,
contentType: false,
data: formData,
async: false,
});
2. Spring控制器(未工作)
@PutMapping("{pofolNo}/main-image")
public CommonApiResponse changePortfolioMainImage(
@PathVariable("pofolNo") Integer pofolNo,
@RequestParam("autoSelect") String autoSelect,
@RequestParam("mainImage") MultipartFile mainImage) {
log.debug("check : {} / {} / {}", pofolNo, autoSelect, mainImage);
return ok(null);
}
上面的代码会导致 400 错误,表明 autoSelect 参数不存在。
所以我检查HttpServletRequest.getParameter("autoSelect")
像这样。
3. Spring控制器(已工作)
@PutMapping("{pofolNo}/main-image")
public CommonApiResponse changePortfolioMainImage(
@PathVariable("pofolNo") Integer pofolNo,
HttpServletRequest request,
@RequestParam("mainImage") MultipartFile mainImage) {
log.debug("check : {} / {} / {}", pofolNo, request.getParameter("autoSelect"), mainImage);
return ok(null);
}
上面的代码结果是成功的。
有什么不同?我不明白@RequestParam
没有工作但与HttpServletRequest
.