ИЗМЕНИТЬ ИЗОБРАЖЕНИЕ в Srping Boot Controller - PullRequest
1 голос
/ 03 августа 2020

В разделе редактирования написал, как заставить загружать изображение

Спасибо большое

@PostMapping("/save")
public String add(@ModelAttribute("category") Category category, RedirectAttributes ra,
        @RequestParam("fileImage") MultipartFile multipartFile) throws IOException {
    String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
    category.setPhoto(fileName);

    Category saveCategory = categoryService.save(category);
    String uploadDir = "./category-logos/" + saveCategory.getId();
    Path uploadPath = Paths.get(uploadDir);
    if (!Files.exists(uploadPath)) {
        Files.createDirectories(uploadPath);
    }

    try (InputStream inputStream = multipartFile.getInputStream()) {
        Path filePath = uploadPath.resolve(fileName);
        Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new IOException("could not save upload file: " + fileName);
    }

    return "redirect:/category/list";

}
@GetMapping("/edit/{id}")
public String edit(Model model, @PathVariable(name="id")Long id) {
    //`**`**`**enter code here**`**`**`
    
    
}
...