Java 如何在Spring Boot中创建上载文件的列表?

Java 如何在Spring Boot中创建上载文件的列表?,java,spring,spring-boot,Java,Spring,Spring Boot,我想创建一个上传文件列表,这些文件存储在我硬盘上的一个目录中 我的控制器: @Controller class MyFileUploadController { @RequestMapping(value = "/uploadOneFile", method = RequestMethod.GET) public String uploadOneFileHandler(Model model) { MyUploadForm myUplo

我想创建一个上传文件列表,这些文件存储在我硬盘上的一个目录中

我的控制器:

@Controller 
class MyFileUploadController {


    @RequestMapping(value = "/uploadOneFile", method = RequestMethod.GET)
    public String uploadOneFileHandler(Model model) {

        MyUploadForm myUploadForm = new MyUploadForm();
        model.addAttribute("myUploadForm", myUploadForm);

        return "uploadOneFile";
    }


    @RequestMapping(value = "/uploadOneFile", method = RequestMethod.POST)
    public String uploadOneFileHandlerPOST(HttpServletRequest request, //
                                           Model model, //
                                           @ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {

        return this.doUpload(request, model, myUploadForm);

    }


    @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.GET)
    public String uploadMultiFileHandler(Model model) {

        MyUploadForm myUploadForm = new MyUploadForm();
        model.addAttribute("myUploadForm", myUploadForm);

        return "uploadMultiFile";
    }


    @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
    public String uploadMultiFileHandlerPOST(HttpServletRequest request, //
                                             Model model, //
                                             @ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {

        return this.doUpload(request, model, myUploadForm);

    }

    private String doUpload(HttpServletRequest request, Model model, //
                            MyUploadForm myUploadForm) {

        String description = myUploadForm.getDescription();
        System.out.println("Description: " + description);


        String uploadRootPath = request.getServletContext().getRealPath("upload");
        System.out.println("uploadRootPath=" + uploadRootPath);

        File uploadRootDir = new File("(directory)");

        if (!uploadRootDir.exists()) {
            uploadRootDir.mkdirs();
        }
        MultipartFile[] fileDatas = myUploadForm.getFileDatas();

        List<File> uploadedFiles = new ArrayList<File>();
        List<String> failedFiles = new ArrayList<String>();

        for (MultipartFile fileData : fileDatas) {


            String name = fileData.getOriginalFilename();
            System.out.println("Client File Name = " + name);

            if (name != null && name.length() > 0) {
                try {

                    File serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name);

                    BufferedOutputStream stream = new BufferedOutputStream(new 
                    FileOutputStream(serverFile));
                    stream.write(fileData.getBytes());
                    stream.close();

                    uploadedFiles.add(serverFile);
                    System.out.println("Write file: " + serverFile);
                } catch (Exception e) {
                    System.out.println("Error Write file: " + name);
                    failedFiles.add(name);
                }
            }
        }
        model.addAttribute("description", description);
        model.addAttribute("uploadedFiles", uploadedFiles);
        model.addAttribute("failedFiles", failedFiles);
        return "uploadResult";
    }
}

用户可以将其文件上载到uploadOneFile.html上

uploadOneFile.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:c="http://java.sun.com/xml/ns/javaee">

<head>
    <meta charset="UTF-8">
    <title>Upload One File</title>
</head>

<body>
<th:block th:include="/_menu"></th:block>

<h3>Upload single file:</h3>

<form th:object="${myUploadForm}" method="POST"
      action="" enctype="multipart/form-data">
    Beschreibung:
    <br>
    <input th:field="*{description}" style="width:300px;"/>
    <br/><br/>
    File to upload: <input th:field="*{fileDatas}" type="file"/>
    <br/>
    <input type="submit" value="Upload">
</form>
</body>
</html>

上传一个文件
上载单个文件:
贝施雷邦:



要上载的文件:
然后,上传的文件应显示在索引页面上。此外,只需单击文件,就可以下载这些文件


我是一个初学者,你能帮我吗?如果您需要更多信息,请告诉我。

您可以在该页面上创建一个表(您可以根据设计等选择html布局)

主要逻辑可以是:-

  • 从目录中获取文件的列表
  • 将文件名存储在集合、列表或您选择的内容中
  • 通过索引页控制器使用某种模型将上一个列表传递到UI上
  • 呈现文件列表
  • 单击特定文件后,调用端点按名称下载文件
一些最初感兴趣的代码可能如下所示:-


File directoryPath = new File("D:\\PATH\\OF\\DIRECTORY");
FileFilter textFilefilter = new FileFilter(){
    public boolean accept(File file) {
    boolean isFile = file.isFile();
        if (isFile) {
           return true;
        } else {
           return false;
        }
    }
};
//List of all the files (only files)
File filesList[] = directoryPath.listFiles(textFilefilter);
System.out.println("List of the files in the specified directory:");
for(File file : filesList) {
    System.out.println("File-name: "+file.getName());
    System.out.println("File-path: "+file.getAbsolutePath());
    System.out.println("Size: "+file.getTotalSpace());
    System.out.println(" ");
}
 

File directoryPath = new File("D:\\PATH\\OF\\DIRECTORY");
FileFilter textFilefilter = new FileFilter(){
    public boolean accept(File file) {
    boolean isFile = file.isFile();
        if (isFile) {
           return true;
        } else {
           return false;
        }
    }
};
//List of all the files (only files)
File filesList[] = directoryPath.listFiles(textFilefilter);
System.out.println("List of the files in the specified directory:");
for(File file : filesList) {
    System.out.println("File-name: "+file.getName());
    System.out.println("File-path: "+file.getAbsolutePath());
    System.out.println("Size: "+file.getTotalSpace());
    System.out.println(" ");
}