先导包
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.7</version>
</dependency>
Blob 转 MultipartFile 类型
/*** Blob 转 MultipartFile 类型* @param blob Blob* @fileName 带后缀的文件名称* @return MultipartFile* @throws Exception*/
public static MultipartFile blobToMultipartFile(String fileName, Blob blob)
throws Exception{try (InputStream is = blob.getBinaryStream()) {return new MockMultipartFile("file",fileName,ContentType.APPLICATION_OCTET_STREAM.toString(), is);}}
byte字节 转 MultipartFile 类型
public static void byteToFile(byte[] b, String str) {try (InputStream is = new ByteArrayInputStream(b);) {MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), is);// 利用MultipartFile 完整的将文件copy到本地文件夹,不用考虑文件类型file.transferTo(new File(str));} catch (Exception e) {e.printStackTrace();}
}