Home > Enterprise >  Saving and serving images in a Java Web app
Saving and serving images in a Java Web app

Time:07-24

I'm making an ecommerce app for school, the app has a product page that displays a jpg image for each product. The /img folder is inside my project folder (see code for path) and the link in html is just ./img/ img name. The images and products are dynamically added through a form in the admin panel that gives the image and other data as multipart/form-data to my servlet. This is the code that saves the image in the aforementioned folder

                Part filePart = request.getPart("slika");
                String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
                                           // Ugly absolute path
                File filePath = new File("C:\\Users\\dgudo\\OneDrive\\Desktop\\Faks\\Sesti semestar\\Internet Programerski alati\\Projekat\\CateringService\\web\\img");
                File file = new File(filePath, fileName);                

                try ( InputStream fileContent = filePart.getInputStream()) {
                    
                    Files.copy(fileContent, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException ie) {
                    request.setAttribute("msg", ie.getMessage());
                }

This works, but i'm using that ugly absolute path that's unique to my local machine. Using a relative path doesn't work, it tells me the required resource couldn't be found, and using System.getProperty("user.dir") returns the path of the Tomcat server running the web app, where it obviously doesn't find anything.

So i've got a few questions:

  1. Without internet access (environment I'll be presenting the app in at uni) what's the best way of saving and serving the jpg's,

    1.1 Should I leave it as is and hope the professor doesn't see the path (there's 2000 lines of code in the project, easy to miss)?

    1.2 Should I move my /img folder to the path given by System.getProperty("user.dir") and serve/save there. If yes, how can I set the result of System.getProperty("user.dir") as a global variable somewhere so I can use it inside JSP code?

  2. In an actual production environment where are images saved and served from?

Thanks.

CodePudding user response:

Question 1:

1.1 : is a good approach, it should only be revised

1.2 : if you like to user home directory you should use System.getProperty("user.home") instead of System.getProperty("user.dir")

Normally, while the launch of the application you should create a temporary folder myapplicationimp" on user.home directory in which you manage the images.

EX : you can use @PostConstruct if you use springframework that check and create the folder instantly after bean creation (still an example).

Question 2:

In production save data we use the same aproache that you used, or a third-party environnement to like distant repositories (as google drive) or databases.

hope this will help

  • Related