How to convert a multipart file to File?

后端 未结 9 1984
有刺的猬
有刺的猬 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条回答
  •  天命终不由人
    2020-11-29 17:27

    if you don't want to use MultipartFile.transferTo(). You can write file like this

        val dir = File(filePackagePath)
        if (!dir.exists()) dir.mkdirs()
    
        val file = File("$filePackagePath${multipartFile.originalFilename}").apply {
            createNewFile()
        }
    
        FileOutputStream(file).use {
            it.write(multipartFile.bytes)
        }
    

提交回复
热议问题