【Java Web】MultipartFile和byte[]互转

接口接收文件
@RequestParam("file") MultipartFile file

MultipartFile转byte[]

String name = file.getName();
String originalFilename = file.getOriginalFilename();
String contentType = file.getContentType();
String bytes = file.getBytes();

byte[]转MultipartFile

InputStream inputStream = new ByteArrayInputStream(bytes);
MultipartFile file = new MockMultipartFile(name, originalFilename, contentType, inputStream);

String转MultipartFile

FileInputStream fileInputStream = new FileInputStream(path);
byte[]bytes = IOUtils.toByteArray(fileInputStream);
InputStream inputStream = new ByteArrayInputStream(bytes);
MultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "image/jpeg", inputStream);

 

posted @ 2023-05-23 16:14  翠微  阅读(1152)  评论(0编辑  收藏  举报