在开始我的问题之前,英语是不准确的,因为我是韩国人。
我使用Spring 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.弹簧控制器(未工作)
@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.弹簧控制器(已工作)
@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一起工作。
发布于 2018-09-25 06:01:14
第二个不能工作,因为@RequestParam("autoSelect")字符串autoSelect需要null;
但必须提供默认值或require=false。
第三个总是工作的,因为它只注入HttpServletRequest。但是请小心,这个值可能仍然是空的。
如何在多部分/表单数据中获取参数值,并依赖于servlet版本,这是完全不同的。
更清楚地解释了多部分和servlet版本:
https://stackoverflow.com/questions/52484422
复制相似问题