How to convert a multipart file to File?

后端 未结 9 1997
有刺的猬
有刺的猬 2020-11-29 16:44

Can any one tell me what is a the best way to convert a multipart file (org.springframework.web.multipart.MultipartFile) to File (java.io.File) ?

In my spring mvc w

9条回答
  •  猫巷女王i
    2020-11-29 17:31

    You can also use the Apache Commons IO library and the FileUtils class. In case you are using maven you can load it using the above dependency.

    
        commons-io
        commons-io
        2.4
    
    

    The source for the MultipartFile save to disk.

    File file = new File(directory, filename);
    
    // Create the file using the touch method of the FileUtils class.
    // FileUtils.touch(file);
    
    // Write bytes from the multipart file to disk.
    FileUtils.writeByteArrayToFile(file, multipartFile.getBytes());
    

提交回复
热议问题