Overwriting images when deploying using Spring - java

I have the following problem: I haven an application which uses SpringMVC and Hibernate, and when the user uploads an image it stores it in the /resources/img folder, but when I have to do another deploy I complile my source code into a war, and then I deploy it again, overwriting the image that the user has just upload becose the whole folder is in the war. I don't know how it works, but the problem is that the user lose every image that he has uploaded when I deploy the app again.
Does anyone know the best way to solve this? I know that save the image in the database is not a good practice, so I want to avoid that.
Thank you!

Related

saving images to static folder require refresh/reload the server to implement changes

I'm trying to save/load images in the resource/public/images folder of my spring boot project, but I faced a problem that all changes to this folder require to restart of the tomcat server but I need to implement all changes without restarting anything to avoid production problems.
I searched for a solution on the Internet and I reached to use additional tools that should be a solution to my problem so I used devtool dependency but I still have the problem.
I'm not looking for local changes on my IDE but I need a solution that can be work everywhere.
First of all the solution that you are looking for is not possible and should not be design like that. Think of it, when your application would be ready, it would be a jar/war (an archived library) deployed on production and its not expected that your jar/war has to have changed at runtime. The correct solution would be, on the same server where you would deploy you app, you can create a folder (may be in same directory where you place your jar) and in this folder you can manage your images and can be read from there. So try by creating a folder outside your app.
And for your local, devtool is basically consume the latest changes from code and helpful only for development as you don't want to restart the server every time when you make any changes in the code.

Spring-boot, thymeleaf load uploaded image without application server restart

My question is, what is the best practice to store images with spring boot and thymeleaf, if I want to load the image without the restart of the application. At the moment I'm able to upload the image so I see it in the uploaded folder and in the database I've stored It's name, But then when I refresh the page the Image is not loaded. I'm retrieving the image standard way in thymeleaf <img th:src="#{'../images/'+${product.image}}". The path is correct because after restart of the server the image is loaded.I guess there is a problem with static folder that It's included in jar and couldn't be changed. But what is the best practice to solve this kind of problem in spring boot. Thanks
The problem is the static directory is loaded at startup. So any changes files added after the ApplicationContext is finished. You probably would be interested in this post: Refreshing static content with Spring MVC and Boot I would look past Dave Syer's post as he is talking about the IDE only, but the answer below that by Steve should help you.

Best way to set up a program with a lot of pictures

I'm making a small program that will make the user chose between about a thousand pictures (thumbnails) and then display this picture in full size. I have the pictures saved on both my computer and on a CD, so how do I go about "importing" these to the program? Is the best way to put all the pictures in one huge picture and import it? And if I wish to make the program run on somebody else's computer, what do I do then?
If you are trying to use this application on another computer i would put the pictures on some sort of cloud service that can be reached anywhere then jar up the java application and point to the file path of those images. Hope this helped :)
To ship files with an application, one can put files in a package along with sources and read a file with ClassLoader.getResourceAsStream.
If you are using maven or gradle for build/project, put the package under src/main/resources and not in the src/main/java folder.
Note on Android this may not work, put images in assets folder and use Context.getAssets().open.
If you have a huge database, I would advise to a webdatabase software (SQL like). Here is a tutorial on how to use a SQL database with java; https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
I hope this gives you ideas on how to solve this issue.

Openshift context path

I have deployed the war into tomcat 7 Openshift . Everything is fine and its running .Myappp Here is my mapped web application.
I am storing images in the dir webapps->docs->images
But When I upload the image (with UI) it doesnt appear on site and I cant even find where images are stored (I connected using FTP -filezilla )
Where my deployed war file stored ? Do I need to make any changes in my code so to upload in proper dir of Openshift??
Thanks in advance
You should use your OPENSHIFT_DATA_DIR (~/app-root/data) to store uploaded images. However, using Java this presents some challenges. I would recommend that you read through this article (https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift), it should help you with dealing with user uploaded images.

Uploading and saving files in JSP

I'm trying to upload images in JSP using Apache Common FileUpload with Spring/hibernate. Uploading of images works well.
My project folder is located by the following path.
E:\Project\SpringHibernet\wagafashionNew\wagafashion
After parsing the request, I'm trying to save the uploaded image into the following folder.
E:\Project\SpringHibernet\wagafashionNew\wagafashion\web\images
I've tried in various ways to get this path but I couldn't succeed.
Specifying a relative path something like the following
File f=new File("wagafashion/web/images/image_file.xxx");
would not work.
Is there a way to retrieve the following path?
E:\Project\SpringHibernet\wagafashionNew\wagafashion\web\images
or specify a relative path with the new File("relative_file_path") constructor?
Am I saving files into a wrong directory? In that case in which project folder files are to be saved?
Maybe.
One way it to ask the the ServletContext to getRealPath("/web/images"), and see if that returns something -- it doesn't have to, but it likely will. If it does, then you can put the images there.
However.
If you're deploying like most folks using a WAR, then all of those images will Go Away as soon as you redeploy, as most containers take the WAR to be deployed and explode it on to the file system. Whatever was in the directory before you did this (i.e. the code and artifacts from when you last deployed) will be going bye bye, and so you will "lose" your images.
You can mitigate this by doing a directory deploy, that is deploy an already exploded directory. Then you KNOW where the application is located (since you put it there). Then it's up to you to sync that directory with your new code as you make changes (notably it's up to you to delete old stuff you don't want any more).
Other than that, different containers have different mechanisms for mapping in an external directory in to the application space. Glassfish has the concept of "alternate doc roots" that you can use. This allows you to have a place out side of the deployment where static stuff can live and still be served by the container, but isn't wiped out when you redeploy.
Finally, you can always do that yourself, stream your own images, etc. without relying on the container at all. This way you can put the images on the file system, in the database, in memory, whatever.

Categories

Resources