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) { System.out.println ( "Exception" ); } if (file.getSize() > 1024 * 1024 * 1) { System.out.println ( "Exception" ); } //获取文件后缀 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) { System.out.println ( "Exception" ); } // 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) { System.out.println ( "Exception" ); } // 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(); Object the Properties = FactoryBean.getObject ();
writing configuration information in the configuration file path savePath = (String) object.get("savePath"); } }
This is the above tools, you need to configure upload path information in the configuration file