public class UploadImgUtils { private static String savePath = ""; /** * Upload photos Tools * * @Param File file * @Param workNo work order number * @Param Property configuration environment (dev, prod, test) * @return * @throws OperationException */ public static String uploadImg(MultipartFile file, String workNo, String property) throws OperationException { if (file == null) { throw new OperationException(ReturnCodeEnum.OPERATION_IMG_IS_NULL); } if (file.getSize() > 1024 * 1024 * 1) { throw new OperationException(ReturnCodeEnum.OPERATION_IMG_SIZE_LARGE); } //获取文件后缀 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) { throw new OperationException(ReturnCodeEnum.OPERATION_IMG_FORM_ERROR); } // to savePath been assigned getProperties (property); SavePathFile File = new new File (savepath); IF (! SavePathFile.exists ()) { // If the existence of the directory, create a directory savePathFile.mkdir (); } // labor single number as a unique identifier String filename = workNo + + "." Suffix; the try { // save the file in the specified directory file.transferTo ( new new File (savepath + filename)); } catch (Exception e) { throw new OperationException(e, ReturnCodeEnum.OPERATION_SAVE_IMG_ERROR); } // Returns the file name of the return savepath + filename; } /** * Reads the configuration information in the file. * * @return */ private static void getProperties(String name) { YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean(); factoryBean.setResources(new ClassPathResource("application-" + name + ".yml")); factoryBean.afterPropertiesSet(); Properties object = factoryBean.getObject(); savePath = (String) object.get("operation.savePath"); } }
Upload file above, the following method is used to obtain the environment variable configuration file