1

My problem is that when I upload an image from one jsp and I go to another jsp which should show the image uploaded it doesn't show it. The image is uploaded and saved in my server, and I reference the correct path but for seeing the image I just need to restart tomcat. Here is my uploader servlet(I think that it works correctly because the image is saved):

        // Get file from form
        Part part = request.getPart("cartel");  
        // Path     
        String ruta_local = getServletContext().getInitParameter("local_path");
        String img_dir = getServletContext().getInitParameter("img_directory");

        // Image name
        String fileName = extractFileName(part);

        // Add random number to image name
        Random rand = new Random();
        int random_numbers = rand.nextInt(1000);

        fileName = NameOrExtension(fileName, "nombre") + String.format("%03d", (random_numbers))
        + "." + NameOrExtension(fileName, "extension");
        request.getSession().setAttribute("foto", fileName);

        // Final path
        String cartel_path = ruta_local + File.separator
                + img_dir + File.separator + fileName;      


        File cartel = new File(cartel_path);

        // File write
        try (InputStream input = part.getInputStream()){
            Files.copy(input, cartel.toPath());
            input.close();
        }

The images are saved in my img-carteles forlder which is in WebContent folder, and when I try to show an image knowing his name I use:

<img class="card-img-top" src="img-carteles/image_name" alt="Card image cap">

So I think that is a tomcat's rendering issue. Question: How can I show and uploaded image not having to restart tomcat?

Juan Ortega
  • 69
  • 1
  • 9
  • 1
    And what is the code of the servlet mapped to img-carteles, loading the image from the directory, and sending its bytes to the response? If you don't have such a servlet, there is your problem: do NOT store the data in tomcat's webapp directory. Store them in an external directory, and write such a servlet to read them from that directory. You don't want to lose all these images when you deploy the next version of your app. The images are data,. – JB Nizet Nov 26 '18 at 18:31
  • Also, you're using the wrong naming convention. In Java, we don't use underscores for variable names - random_numbers should be randomNumbers – mwieczorek Nov 26 '18 at 19:50
  • @JBNizet I don't understand why you must buffer the image through a servlet instead of access to it like with a normal image (not uploaded by an user). Is it because when you launch tomcat it saves all the web content in memory and recent uploaded content isn't in it? Thank you for answering – Juan Ortega Nov 26 '18 at 21:16
  • 1
    Not in memory, but in a work folder. But most importantly, uploaded files are data, that shouldn't disappear if you redeploy or reinstall tomcat. And you should be able to serve that data from several clustered instances of tomcat. And check permissions before serving them, too. It's data, just lie the data submitted by forms and saved in a database. – JB Nizet Nov 26 '18 at 22:28

1 Answers1

0

The answer to this is the same as @BalusC's answer in Uploaded image only available after refreshing the page

In a nutshell, uploaded images should not be saved in the deployment folder.

Just changing the directory of the img-carteles directory to be outside the deployment folder should solve the problem.

gordon613
  • 2,770
  • 12
  • 52
  • 81