0

when i add image on admin page for a product . it is uploading successfully and uploaded image is reflected (saves) on correct folder

But when i run user portal and sees products . The product images are not showing up

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addBookPost(@ModelAttribute("book") Book book, HttpServletRequest request) {
    bookService.save(book);

    MultipartFile bookImage = book.getBookImage();

    try {
        byte[] bytes = bookImage.getBytes();
        String name = book.getId() + ".png";
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File("src/main/resources/static/image/book/" + name)));
        stream.write(bytes);
        stream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "redirect:bookList";
}

Product Page image issue < you can find it Here>

1 Answers1

0

The issue is with the path where you are saving the images.

When the application is running, it ignores the src directory and refers to the target directory and your images are being uploaded in src/main/java/resources/static/image. If you copy the images manually to the path target/classes/static/image, the images will be displayed properly.

And I absolutely do not recommend saving the images in the target directory, because doing a maven clean will delete that directory. To give it a try, open a terminal/command prompt on the root directory of your project and execute the command mvn clean.

I would strongly advice you to save the images somewhere else, like 'C:\projectName\images' and make the path configurable via application.properties or via a configuration saved in the db.

atish.s
  • 1,534
  • 11
  • 19