如何使用Spring上传目录/文件夹包含文件?

时间:2018-08-21 17:41:03

标签: java spring file-upload

我一直试图将文件夹上载到服务器(java控制器文件中的指定目录)。下面是代码

<div id="myRadioGroup">
File upload <input type="radio" name="filefolder" checked="checked" value="file" />
Folder upload <input type="radio" name="filefolder" value="folder" />
<div id="file" class="desc">
    <div class="form-group">
        <input type="file" class="form-control" id="file"
                       placeholder="Add additional files or folders" name="fileToUpload" >
    </div>
</div>
<div id="folder" class="desc">
    <div class="form-group">
        <input type="file" class="form-control" id="folder"
                       placeholder="Add additional files or folders" name="folderToUpload" webkitdirectory mozdirectory directory multiple>
    </div>
</div>
</div>

Spring Controller代码为

@PostMapping("/terminal/upload")
public String singleFileUpload(@RequestParam("filefolder") String filefolderselection,
        @RequestParam("fileToUpload") MultipartFile file,
        @RequestParam("folderToUpload") MultipartFile[] files, RedirectAttributes redirectAttributes) {

    System.out.println("filefolderselection " + filefolderselection);
    if (filefolderselection.equals("file")) {

        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("msg", "Please select a file to upload");
            return "redirect:/terminal";
        }

        try {

            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);

            redirectAttributes.addFlashAttribute("msg",
                    "You successfully uploaded '" + file.getOriginalFilename() + "'");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if (filefolderselection.equals("folder")) {
        StringJoiner sj = new StringJoiner(" , ");

        for (MultipartFile f : files) {

            if (f.isEmpty()) {
                continue; // next pls
            }

            try {

                byte[] bytes = f.getBytes();
                System.out.println("File PATH GETNAME ="+f.getName()+" Original path Name" + f.getOriginalFilename());
                Path path = Paths.get(UPLOADED_FOLDER + f.getOriginalFilename());
                Files.write(path, bytes);

                sj.add(f.getOriginalFilename());

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        String uploadedFileName = sj.toString();
        if (StringUtils.isEmpty(uploadedFileName)) {
            redirectAttributes.addFlashAttribute("msg", "Please select a folder to upload");
        } else {
            redirectAttributes.addFlashAttribute("msg", "You successfully uploaded '" + uploadedFileName + "'");
        }

    }

    return "redirect:/terminal";
}

现在,我可以上传文件夹选择所选择的多个文件。但是我无法维护要上传的目录结构,因为MultipartFile类没有任何获取方法来知道正在选择和提交的文件/文件夹路径?任何想法必须做什么以获取完整的文件名,以便我可以在创建文件之前构建类似的文件结构。注意:我采用了类似的方法,并在Django中实现。

0 个答案:

没有答案