Я пытаюсь загрузить файлы на сервер, но каждый раз, когда я запускаю приложение, он создает папку на сервере, а затем загружает файл в эту папку. Как хранить файлы, которые загружены постоянно, чтобы при каждом запуске приложения он брал существующие файлы для дальнейших функций или спросите пользователя, нет ли его на сервере для загрузки.
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String index(Model model, @RequestParam("file") MultipartFile[] files,RedirectAttributes rm) {
List<String> ls = new ArrayList<>();
try {
for (int i = 0; i < files.length; i++) {
if (!files[i].isEmpty()) {
System.out.println("sdfghjk");
ls.add(files[i].getOriginalFilename());
byte[] bytes = files[i].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 + files[i].getOriginalFilename());
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
logger.info("Server File Location=" + serverFile.getAbsolutePath());
} else {
rm.addFlashAttribute("error","all the fies are needded");
return "redirect:/uploadForm";
}
}
model.addAttribute("list", ls);
model.addAttribute("message", "succsesfully uploaded");
return "NewFile";
} catch (Exception e) {
model.addAttribute("message", "failed to upload");
System.out.println("catch");
return "NewFile";
}
}