private boolean isInvalidIcon(TerminalType terminalType, int imgWidth, int imgHeight) { String checkUploadImage = configService.getByName(ConfigConstant.CHECK_UPLOAD_IMAGE_SPECIFICATION); if ("true".equals(checkUploadImage)) { return imgWidth != terminalType.getIconWidth() || imgHeight != terminalType.getIconHeight(); } return false; }
private void checkIconAndSaveFile( Long id, TerminalType terminalType, MultipartFile file, String targetDir) throws ServiceException { String originalFilename = file.getOriginalFilename(); String tmpName = uploadFilePathService.getTargetTmpFilename(id, originalFilename); String uploadedTmpFilePath = saveUploadFile(file, targetDir, tmpName); log.debug("targetDir:{} uploadedFilePath:{}", targetDir, uploadedTmpFilePath); if (saveFileSuccess(uploadedTmpFilePath)) { // 判断文件分辨率是否与对应的终端类型匹配,如果分辨率不合法,则删除文件,提示分辨率不正确 try { File imgFile = new File(uploadedTmpFilePath); FileInputStream fis = new FileInputStream(imgFile); BufferedImage bufferedImg = ImageIO.read(fis); int imgWidth = bufferedImg.getWidth(); int imgHeight = bufferedImg.getHeight(); fis.close(); log.debug("imgWidth:{},imgHeight:{}", imgWidth, imgHeight); if (isInvalidIcon(terminalType, imgWidth, imgHeight)) { boolean deleteImgFile = imgFile.delete(); log.debug("deleteImgFile {} ok?:{}", uploadedTmpFilePath, deleteImgFile); throw new ServiceException( terminalType.name() + "图片分辨率不符合规定,上传图片规格必须为" + terminalType.getIconWidth() + "*" + terminalType.getIconHeight()); } else { String targetFilename = uploadFilePathService.getTargetFilename(id, originalFilename); log.debug( "targetDir:{} targetFilename:{} uploadedFilePath:{}", targetDir, targetFilename, uploadedTmpFilePath); File validTmpFile = new File(uploadedTmpFilePath); File oldIcon = new File(targetDir + targetFilename); if (oldIcon.exists()) { oldIcon.delete(); } FileUtils.moveFile(validTmpFile, new File(targetDir + targetFilename)); } } catch (FileNotFoundException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } } }