1. 程式人生 > >SpringBoot 上傳文件夾

SpringBoot 上傳文件夾

cal seda tle ons sed local tran body eth


前端代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>upload</title>
</head>
<body>
<form action="http://localhost:8080/api/upload" enctype="multipart/form-data" method="post">
    <input id="file" type="file" name="files"
multiple webkitdirectory /> <input type="submit" value="上傳文件夾" /> </form> </body> </html>

後端代碼:

@RequestMapping("/api/upload")
public class UploadController {

    @PostMapping
    public ResponseData<?> folder(MultipartFile[] files) throws IOException {
        for
(MultipartFile file : files) { //上傳文件目錄 String uploadFolder = "D:/upload_test"; String fileName = file.getOriginalFilename(); File uploadFile = new File(uploadFolder,fileName); //判斷上傳文件目錄是否存在,如果不存在就創建 if (!uploadFile.getParentFile().exists()) { uploadFile.getParentFile().mkdirs(); } file.transferTo(uploadFile); }
return new ResponseData<>().success(); } }

是不是覺得很簡單哉,那就趕快自己動手試一試吧!

SpringBoot 上傳文件夾