场景:表单带有图片文件,只修改文字不修改图片提交报错。
代码:
public ResponseEntity<Object> updateAdvertisement(@Validated Advertisement resources,@RequestParam MultipartFile file){
advertisementService.findById(resources.getId());
try{
if (file != null){
List<String> fileNames = imageUtils.upload(new MultipartFile[]{file},imagePath);
resources.setTitlePicture(fileNames.get(0));
}
advertisementService.update(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (IOException e) {
throw new BadRequestException(e.getMessage());
}
}
原因:加了@RequestParam注解使得file变成了必须上传,所以在修改上传的时候没有传file图片文件会进行报错。