How to convert MultipartFile to in Spring

Views: 851   

If you live in Spring world you might know that org.springframework.web.multipart.MultipartFile is the representation of an uploaded file received in a multipart request. But in some cases, you may want to convert this into one such example can be what if you want to store the file into MongoDB? as MongoDB's APIs accepts
With the following utility method, you can easily convert MultipartFile into Java File

public static File convert(MultipartFile file) throws IOException {
    File convFile = new File(file.getOriginalFilename());
    FileOutputStream fos = new FileOutputStream(convFile);
    return convFile;
On By