This is my image boot code in Spring Boot:
String root = ctx.getRealPath("/");
File dir = new File(root + File.separatorChar + "images");
if (!dir.exists())
dir.mkdir();
String path = dir.getAbsolutePath() + File.separatorChar
+ product.getProductName() + "."
+ file.getContentType().split("/")[1];
System.out.println(path);
File file1 = new File(path);
try {
FileOutputStream fod = new FileOutputStream(file1);
fod.write(file.getBytes());
fod.close();
product.setProductPicture("/images/" + product.getProductName()
+ "." + file.getContentType().split("/")[1]);
} catch (IOException e) {
e.printStackTrace();
}
File uploading works fine, the only problem with this code is that when I use it ctx.getRealPath("/"), it returns a temporary location, and when I reload the spring boot application, I lose existing files that have already been downloaded since it creates a new temporary directory.
This causes some problems, since I also have to display these pictures on my site, and now it returns “image error not found”.
Therefore, I needed a solution that would allow me to upload files in a permanent place and serve them from there in a browser.
Note. I use the timeline for viewing.