Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created November 8, 2018 10:07
Show Gist options
  • Save CaiJingLong/5bc73eeea6aea72fc738980a540924d5 to your computer and use it in GitHub Desktop.
Save CaiJingLong/5bc73eeea6aea72fc738980a540924d5 to your computer and use it in GitHub Desktop.
receive file success
package com.kikt.demo
import org.springframework.web.bind.annotation.RequestMapping
import java.io.IOException
import com.sun.tools.corba.se.idl.toJavaPortable.Util.mkdir
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import java.io.File
@RequestMapping("/api")
@RestController
class TestFileUploadController {
@RequestMapping("/test")
fun upload(@RequestParam("file") file: MultipartFile): String {
if (file.isEmpty()) {
return "false"
}
val fileName = file.getOriginalFilename()
println("fileName = $fileName")
val size = file.getSize()
val path = "/Users/cai/Documents/upload"
val dest = File(path, File(fileName).name)
if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
dest.getParentFile().mkdir()
}
dest.createNewFile()
file.transferTo(dest)
return "file name = $fileName , size = $size , "
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment