MultipartFile 不上传文件报错问题
实例代码如下:
public static final String upload(String baseDir, MultipartFile file) throws IOException
{
try
{
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e.getMessage(), e);
}
}
如上述代码不上传文件,会报空指针异常,可修改为以下代码:
@RequiresPermissions("zhxc:personnel:add")
@Log(title = "人才信息", businessType = BusinessType.INSERT)
@PostMapping("/add")
@ResponseBody
public AjaxResult addSave(HttpServletRequest request,ZhxcVillagePersonnel zhxcVillagePersonnel)
{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");
if(file != null ) {
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName;
try {
fileName = FileUploadUtils.upload(filePath, file);
} catch (IOException e) {
return AjaxResult.error("添加失败!");
}
zhxcVillagePersonnel.setPeImgurl(fileName);
}
return toAjax(zhxcVillagePersonnelService.insertZhxcVillagePersonnel(zhxcVillagePersonnel));
}