一、使用MockMultipartFile类进行转换
这个方法比较简单,代码如下:
File pdfFile = new File("D://test.pdf");
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
使用MockMultipartFile需要引入spring-test的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
二、使用MockMultipartFile类进行转换
可以看MockMultipartFile的构造方法