SpringBoot 上傳文件夾


 

前端代碼:html

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

}

 

是否是以爲很簡單哉,那就趕快本身動手試一試吧!web

相關文章
相關標籤/搜索