gpt4 book ai didi

javascript - 如何使用 spring 和 jsp 在列表中显示上传的文档

转载 作者:行者123 更新时间:2023-11-29 23:19:09 25 4
gpt4 key购买 nike

@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.";
}
}

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

最佳答案

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

关于javascript - 如何使用 spring 和 jsp 在列表中显示上传的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51394286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com