import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
public static void main(String[] args) {
convertFiletoMultiPart();
}
private static void convertFiletoMultiPart() {
try {
File file = new File(FILE_PATH);
if (file.exists()) {
System.out.println("File Exist => " + file.getName() + " :: " + file.getAbsolutePath());
}
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
IOUtils.toByteArray(input));
System.out.println("multipartFile => " + multipartFile.isEmpty() + " :: "
+ multipartFile.getOriginalFilename() + " :: " + multipartFile.getName() + " :: "
+ multipartFile.getSize() + " :: " + multipartFile.getBytes());
} catch (IOException e) {
System.out.println("Exception => " + e.getLocalizedMessage());
}
}
This worked for me.