MultipartFile 转 File
public File M2F(MultipartFile file) throws Exception { File f=File.createTempFile(UUID.randomUUID().toString(), "." + FilenameUtils.getExtension(file.getOriginalFilename())); file.transferTo(f); return f; }
File 转 MultipartFile
导入
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency>
public MultipartFile F2M(File file) throws Exception { MultipartFile multipartFil; File file = new File("文件路径"); if(file.exists()) { FileInputStream input = new FileInputStream(file); multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input)); } return multipartFil; }