@PostMapping(value = "/uploadDriverImage") public JsonResVo uploadDriverImage(@RequestParam("file") MultipartFile file) { try { //检查文件是否为空 if(file.isEmpty()) { return JsonResVo.buildErrorResult(ErrorEnum.ERROR_DEFAULT.getErrorCode(), "请选择图片"); } //检查文件大小 if(file.getSize() > Constant.M2_TO_BYTE) { return JsonResVo.buildErrorResult(ErrorEnum.ERROR_DEFAULT.getErrorCode(), "请上传2M以内的图片"); } //检查是否是图片 BufferedImage bi = ImageIO.read(file.getInputStream()); if(bi == null){ return JsonResVo.buildErrorResult(ErrorEnum.ERROR_DEFAULT.getErrorCode(), "上传的文件不是图片"); } String originalFilename = file.getOriginalFilename(); String fileType = null; if(originalFilename.contains(".")) { fileType = originalFilename.substring(originalFilename.lastIndexOf(".")+1); } else { fileType = Constant.IMAGE_TYPE_JPG; } String ossFilePath = OSSUtil.upload(file.getInputStream(), System.currentTimeMillis()+"", fileType, "driverimage"); return JsonResVo.buildSuccess(ossFilePath); } catch (Exception e) { e.printStackTrace(); return JsonResVo.buildErrorResult(ErrorEnum.ERROR_DEFAULT.getErrorCode(), e.getMessage()); } }