I am working on a J2EE application in which I need to save user's pictures. my question is where should I save these pictures ? knowing that I want to use a relative path .
NB : I am using glassfish as server .
The best practice here is to save the image inside your web application under web content (make a separate folder) , in this way you will be able to render images in other browsers also (not just in eclipse default's)
and you need to save the complete path of the image into database.
eg : e:\workspace\projectname\webcontent\pic\image.jpg
NB : never store image in database
Related
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.
We have developed a web application using JSP and Servlet. The server we use is Tomcat 7. We have hired a host (Daily Razor) with "private JVM" to launch the application in production level.
Now, in our application, user can visit a particular form, browse for a file in his PC and upload it to the server. But I have a question there; what is the best place to store these files? Mainly there are 3 types of files so we would like to categorize them into, "Office', "Home" and "Other" and create 3 folders for them. But inside which main folder these 3 folders should be made?
The main important this is that these files should not be accessed via a URL (because then anyone can get them ), but a Servlet can. Apart from that, the location (String) should be saved in our MySQL database so the file can be accessed again without an issue.
We have developed the application using Netbeans IDE so the folder structure is like below.
I look forward for your answers.
Use a java property to specify the directory where the files should be stored, and pass it on to tomcat during start up.
Also, it might be a good idea to separate the files per user.
Possible Solution:
-Duser.data.export.dir=D:\users_export\directory
In your java code, read the property
String property = System.getProperty("user.data.export.dir");
Now lets say for user 'A'
String userName = getCurrentUser();
Path userDirHome = Paths.get(property, userName, "Home");<br/>
Now use userDirHome to store the data.
Suggestion: When you store the file location in the DB, ensure that you do not store the complete path, only store the relative path, like "Home/myFile.txt".
This will help you at a later point in time when, there is any change in the directory where the file are stored.
I have a image placed in a jsp. I have the image kid.jpg downloaded and kept in /resources/images folder. The below is the code snippet.
<li style="background-image: url('resources/images/kid.jpg');">
Now my web app is up and running. How to give end user to change that image/ upload a different image in that place. The same with the text in a div.
I am not getting the data from database and adding to the div dynamically.
Is database communication only way in such cases or any other way? I see many web sites contents are changing day by day. Are they modifying the webpages and replacing the existing files/ are they getting the data from database?
Please suggest how to achieve such scenarios.
Thanks
If There is no database on server and you want to allow all users ( unregistered ) to change a specific image, then what you can do is :- store the image file name in cookie, and on server check every time for that cookie, and if does not exists and render the same output with default image, other wise use that cookie's file_name image , and then on client side you can provide a facality to users to select images from particulers images that reside on server, and when user select some image so save that file name in cookie :) , Sorry for spelling mistakes, It's just a overview of solution of your problem :)
I have a non-scalable OpenShift app which uses the jbosseap cartridge and also has MySQL and PhpMyAdmin. I can upload and save image files to folders within the OPENSHIFT_DATA_DIR, e.g OPENSHIFT_DATA_DIR/appimages/uploaded.png but I have not been able to display the uploaded images with the HTML img tag.
There seem to be no way to get a correct path to images uploaded under OPENSHIFT_DATA_DIR. I ssh-ed into the server and found that OPENSHIFT_DATA_DIR, which evaluated to /app-root/data/ (actually /var/lib/openshift/5364c54ce0b8cd80180001f7/app-root/data/ ) was kinda outside the webroot of the app (where the ROOT.war was deployed to) which was /jbosseap/standalone/deployments/ROOT.war
So if the app runs from /jbosseap/standalone/deployments/ how can the app display images stored within OPENSHIFT_DATA_DIR which is /app-root/data/ since /app-root and /jbosseap are siblings of the same parent folder.
I just need to use HTML img tag to display an image uploaded and saved under OPENSHIFT_DATA_DIR e.g /appimages/uploaded.png since using src="/appimages/uploaded.png" for an img tag does not display the image.
One solution could be write a Servlet that would read image from $OPENSHIFT_DATA_DIR and write it to OutputStream. Your servlet would be mapped to /images/* and would serve all images. You can also refer to this question How to configure static resources in jBoss AS 7
I am developing an application that stores data dynamically and displays it as it is generated or uploaded. But the problem is when I am uploading any image then it should be displayed. It's path is also defined perfectly but until I refresh the whole application in eclipse it remains unchanged.
So to update application, I have to refresh it.
My application is Java based and is useing JSP and Servlet. Is there any code that can be used to update or refresh the application?
Right now I am doing it by right click -> refresh or directly F5 it.
Or suppose I want to create directory then where will I have to create it that will be accesses easily without refresh.
When I'll host my application then which place is better for to store data?
I know it's outside the web app but any specific place? Because there is no drive so how can I create or make directory and access that one in my code.
A solution to this is a design pattern called Observer. You should read about it.
Is there any code that can be used to update or refresh the application?
Right now I am doing it by right click -> refresh or directly F5 it.
I think you need to look into the workspace re-fresh option in Eclipse ( was made available from Eclipse version 3.7.x ). You can enable it in Preferences > General > Workspace and select Refresh on access or as per need you can change to whatever setting you require.
When I ll host my application then which place is better for to store data ? I know it's outside the web app but any specific place ? Because there is no drive so how can I create or make directory and access that one in my code.
The location of static content is best if placed outside of the scope of the project itself. Maybe some other directory on your server ( be it any directory ).
I can share what I have normally seen as a trend in different applications that I have personally worked on. We had a separate tomcat server that just hosted all static content ( all media ) and our web application accessed that static-server (as it was named) within a secured network. All hits to static content could only be made via our application server and thus all direct hits were either rejected or not entertained at all.
Edit
I would suggest using a an absolute path
and on windows environment
you will HAVE to use the drive lletter
and specify the path as X:\some\path
if you want to hide your letter drive
due to obvious security reason
I can suggest another idea
Idea 1 : Make a separate drive (lets call it drive F) and make a folder with the name of "static". Then in your application , you just forward all requests to file uploads using this path ( F:\static......) . I would advise loading the directory name from a property file instead of hard-coding it in your code
Idea 2 : If you cant make a separate drive, then make a directory namely "static" on the root of same drive ( C:\static ) . Make a user group and give him read/write permissions on this drive and revoke writing permissions from this user on all other drives ( just in case someone messes up with this user-group). Next do the same thing as above i.e specify this path into your application. One thing is that you would have to run your application with that specific user-group to ensure that the permissions security you have setup can be implemented.
Best practice is to store such data outside the webapp's tree to avoid issues on redeployment.
You can't serve these files directly though, commonly it's done by creating a servlet mapped to eg. images/*; that parse the request URL (eg ) and based on that url fetches and serves. You need to set the correct MIME type for the output, and streamcopy the requested file to the servlet's output.
A well explained example of such a servlet can be found on BalusC's blog.