【问题标题】:How to Show the uploaded documents in a list using spring and jsp如何使用spring和jsp在列表中显示上传的文档
【发布时间】:2018-12-25 22:01:06
【问题描述】:
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        String name=file.getOriginalFilename();
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            String rootPath = System.getProperty("catalina.home");
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath() + File.separator +name );
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

            return "You successfully uploaded file=" + name;
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload  because the file was empty.";
    }
}

我已将文档上传到服务器文件夹,我想显示我已上传的所有文档。

【问题讨论】:

  • 你自己试过了吗?如果是这样,您能否向我们展示一些您如何尝试执行此操作的代码?
  • 我已经分享了我用过的代码。
  • 此代码并未显示任何实际列出文件的尝试,它是您的上传代码。
  • 是的,但我想知道我该怎么做,当我选择任何文档时,它应该显示在 jsp iframe 中。
  • 所以您甚至没有尝试自己研究这个问题,您只是决定立即寻求答案?

标签: javascript spring jsp


【解决方案1】:
File folder = new File("path/to/your/uploaded/files/directory");
File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
      } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
      }
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 2013-08-07
  • 1970-01-01
  • 2021-06-19
  • 2019-05-15
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多