You can access the temporary file in Spring by casting if the MultipartFile interface class is equal to CommonsMultipartFile .
public File getTempFile(MultipartFile multipartFile) { CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile; FileItem fileItem = commonsMultipartFile.getFileItem(); DiskFileItem diskFileItem = (DiskFileItem) fileItem; String absPath = diskFileItem.getStoreLocation().getAbsolutePath(); File file = new File(absPath); //trick to implicitly save on disk small files (<10240 bytes by default) if (!file.exists()) { file.createNewFile(); multipartFile.transferTo(file); } return file; }
To get rid of the trick with files smaller than 10,240 bytes, the maxInMemorySize property can be set to 0 in the @Configuration @EnableWebMvc class. After that, all downloaded files will be saved to disk.
@Bean(name = "multipartResolver") public CommonsMultipartResolver createMultipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("utf-8"); resolver.setMaxInMemorySize(0); return resolver; }
Alex78191 Jul 09 '16 at 11:14 2016-07-09 11:14
source share