【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);