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.
Related
I have a JAVA application that needs to store profile pictures that user uploaded.
My project already finish and works fine.
//in my LOCALHOST i use this path:
File file = new File("C:/myProject/uploads/profile_images");
So, now I want deploy this project, i'm using jelastic environment and the question is:
Where should these files (pictures) be stored in our Jelastic ?
I already tried in the same code but doens't work.
I already tried save the files in WebContent folder, work, but when I expand a new .war file, the files that user has uploaded are overrides.
I read about save files in mySql, is a good idea?
Thank for your atention.
Local filesystem is persistent, but make sure to define in 'volumes' if your node has it to ensure files are kept during image redeploy. You can also use Jelastic storage node, but only worthwhile if you're using multiple application nodes.
See https://docs.jelastic.com/docker-volumes for details about how to use the volumes feature - if your node doesn't have this feature it is not based on Docker (not all node types were converted yet). In that case you can write to the filesystem without any risk of those files going missing (i.e. it will behave the same as a dedicated server or your local dev machine).
I have hosted my JavaEE website ImgEcho in which I have given functionality through which users can upload an image. When I implemented this website on localhost I directly gave the path where the uploaded image would be stored as "C:/ProjectName/images"
However, I cant figure out what should be my path on the hosting server.
P.S: I should ask this question to the customer support of my hosting server but they have trashy service.
Thanks in advance.
Depending on what you want, there are many different things that you can do in this situation.
If you want to have a single, shared image folder, in which you store the images of all users, than I recommend that you create an images directory within your projects root.
Otherwise, if you want to create a image storage that is per-user, than you can do the following:
-Project Root
|-User 1
|-images
|-account
|-User 2
|-images
|-account
This is a structure that contains every user in a separate folder, with the subfolders images, which contains the images for that user, and account, the
storage for that user's account details and other information. This is generally more structured of a setup, and will benefit you long-term, although more advanced.
For a more scalable approach, perhaps in the future if you are willing to scale up your service, I recommend getting a large external network hard drive, or NAS (Network-attached storage), and then using that for all of the image storage. This will be very advanced, and possibly pricey, which is why it would be a long-term decision, that could quite possibly benefit your company in the future, because of the scalability.
Overall, I would generally use an images directory, regardless of the rest of the folder structure, as it is the most logically correct decision for your current situation.
EDIT: To make this setup more secure, I recommend putting the images directory into a custom, unrecognised archive file, and then converting the data when you need to use it, and back again when a change is made. This ensures no sneaky users opening your images because it is stored in a plain folder layout.
Create a folder in your root named "images" and save them in there. You have to adjust the rest of the references.
You can store the images inside a path in your project itself by giving a relative path.
You should use a relative path to store it in the site. You would want something like /images/uploads for your project. The path should be at the root of your web documents. The reason to do so is that if you move the project, things would likely break. Using a relative path ensures that you can move the project. In addition, if this is shared hosting, you won't have to worry about your files getting mixed with others.
What I do is run this code in a jsp :
java.io.File f = new java.io.File ("./");
try{
out.println(" f curr " + f.getCanonicalPath());
}catch(Exception e){
out.println(" Err file :" + e + ", " + f);
}
This tells me the path to the current folder of my web app, on some clouds have seen its /xyz/tomcat/temp on others a longer path.
the path will be along the lines of /usr/public_html/images. Who is your hosting through?
Create two directory, One is for your site and another one is your storage puropose.
Advantages:
Each time an user send a server request to access his data, it will go to search a file or folder in the directory.
through chmod we can give different access permission two them.
if the storage folder is mess up with site, it would be a risque of long page load.
Since you have a JavaEE website, why not just make a controller method responsible for downloading the files and have the return value of the upload function be a path to the download method? That way you can store the image wherever your website has write/read access to. I can give more implementation details on request.
So I have a web app (using apache tomcat server,servlets,eclipse IDE). I wrote code to allow user to edit the XML file via UI. So I used the following code to access the XML through java
String fileName = "/MyXML.xml";
String path = this.getClass().getResource(fileName).toString();
This works fine. I am able to edit the file through UI.
Now I want to let the client download the file. But I am not able to access the file while I am trying to download.
However, if I keep the file inside webapps folder, then I can access the file using the following
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/MyXML.xml");
(Thanku Mr.MK Yong- http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/)
But then If i keep it inside webapp folder, how do I access the file for editing the XML ?
So basically I am either able to edit the file, or I am able to download the file(from webapp folder), or I am able to do both on two different copies of the file. I want to edit the XMl file and be able to download the same. So where do I keep the file and how do I access it?
You should store it in the local resource folder as it is essentially a dynamic resource.
The other thing i recommend is if you know the parameters that will be changed then have a template in resource folder and store the changes in database.
Personally i have it the second way.
e.g.
/yourapp/resource/config_file/xmltemplate.xml
Parameters that can change:
userLocation
folderLocation
colorBase
Stored them in the table:
Table: UserCongifStorage
Columns: userLocation, folderLocation, colorBase
So when i need to use the data from row 1 the logic is:
read in the xml file into a string, replace the variables with data retrieved from database, output it as xml to resource folder.
Then you read for usage.
Hope that helps
If the file is in your 'webapp' folder (I think you mean your application root), then it is already accessiable to everyone by calling hxxp://domainname/appname/MyXML.xml. I would suggest you not to store files that can be edited, inside your app folders, since they will be overwritten if you redeploy your application.
Put them in an external directory and load the contents like you would do with all other files. Doing this you can take control over file permissions easily, too.
Task: Copy Folder and contents from one vdi to another vdi. This application is internally facing within the company.
Method:
In jsp have user browse for folder
The folder selection is in a text box, the folder path is passed into an action class
The folder path is placed into a teradata table
A script is called to query the table for the source path and target path (pre-determined) and make the copy
Due Dilligence: So far I have tried the <input type="file", which selects a file, not a folder. Also, the file path is not passed through due to security reasons. I have read other possible solutions but none work.
Question: Are sevlets a viable solution, and if so, how do I create one?
I'm going to go with no. There are several reasons for this.
A Java Enterprise Edition application (be it a Servlet or Java Server Page) is not supposed to access the file system directly.
It is inherently unsafe to expose internal infrastructure through an external website.
I think you need to break it up a bit more.
Save a list of shares the server has access to in a data store of some sort, like a new teradata table or for a quick proof of concept plain text file (if you're on Linux you can use the output of something like showmount -e localhost).
Let the user pick the src share from a combobox or something similar.
Continue from your step 2.
This gives you two immediately obviously advantages, which may or may not be relevant.
You can use the system without having access to the physical shares.
You can add metadata (like description or aliases).
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.