I have a Spring MVC app which I want to deploy on another users PC via a jar file. How can I allow the user to upload, save and display image files? I am not able to display an image if it's not located in the project folder. Is there a way to solve this?
code for upload:
public ModelAndView newItemAdd (@RequestParam("file") MultipartFile file, HttpSession session, ModelMap modelMap, @ModelAttribute("itemSaveDto") ItemSaveDto dto) throws IOException {
if(!Objects.equals(file.getOriginalFilename(), "")){
String filename=file.getOriginalFilename();
String newPath = uploadPath+filename;
file.transferTo(new File(newPath));
String pic = dbPath+filename;
dto.setPic(pic);
}
if(Objects.equals(file.getOriginalFilename(), "")){
dto.setPic(defaultPic);
}
repo.addItem(dto);
return new ModelAndView("redirect:/");
}
html to display images (thymeleaf refers to function which calls URL from database):
<tr th:each="item: ${items}">
<td> <img th:src="@{${item.getPic()}}" width="100px" height="100px"/></td>