SpringBoot 上傳文件夾



 

前端代碼:

<!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();
    }

}

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM