Standard images/data path on an operating system? - java

As in the title, is there a standard path where to store images on an operating system?
For example, in Java you can get the "temporary folder" using System.getProperty("java.io.tmpdir"), is there a similar way to get a path for images?
If not, is there at least a path for generic data storage?
I'm writing a program that generates .pngs that have to be stored, but I want a standard path where to store them until the user chooses his own folder in the settings menu;
Thanks for your time.

I'm not aware of a OS independent path for storing images. Especially with all the unix distributions this might vary from case to case. It also might change based on the localization of the OS. The best way I'd say would be to ask the user where to store the images OR get the users home directory and then store it in a folder called like your program so it is clear to the user where this folder comes from.

If you want to let the user choose where to save them to begin with, then I would recommend either:
Don't save anything to their system without their explicit say-so; that is, don't save anything to the disk anywhere until they select a location for it to go, or
Save it to the temporary folder until they select a specific location to store the image. This way, your code will be generically portable; where specific files live varies between people, but a temp directory is universal.

normally the current working directory is used
System.getProperty("user.dir");
or you can use a relative directory of the home directory
new File(System.getProperty("user.home"), "Pictures");
https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties()

Related

What should be my path when I want users to upload file on my website?

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.

Platform-independent way to get a path to store program data

Is there a platform-independent way to get a path to store program data in Java?
This is surely a very basic question but I can't seem to find the answer anywhere. I am looking for a path to store user data such as user preferences or historical data input. On Windows we would normally store it in C:\Program Files\APPNAME, C:\Program Files (x86)\APPNAME or C:\ProgramData\APPNAME depending on the OS and architecture. On Unix we could store preferences in /etc/APPNAME or /opt/APPNAME depending on the context. On Mac we have a special cabinet to store program's data. In java, how do we get a location like this in a platform-independent way?
The system property user.home should be pretty standard across most desktop systems.
System.out.println(System.getProperty("user.home"));
Note that this is the user the Java process runs under - so in case of server-side Java process, you would need to store information for the users of your app in your own data structure, as your app's users are not known to the OS.
Regarding a system-wide storage location, you may need to detect the OS version and compute the path. Another problem is that you would most likely need to escalate privileges to write to a system-wide location.
Look to store settings in a (sub-)directory of user.home. Not only is it a place to which the app. would reasonably be expected to have read-write privileges, but is cross-platform.
The sub-directory might be based on the Fully Qualified name or package name of the class to prevent clashes. I.E.
package our.com.main;
Would be written to sub-directory our/com/main.

is it possible to place a myfile.file in C:/Windows/System32/ location at runtime?

I wish to place a file named as myFile.file in
C:/Windows/System32/ location.
Here is use java code to place my file. while I execute my program it throws "Access Denied:C:/Windows/System32/myFile.file".
why is it happening? Is it possible to place in that location?
That (and many other) system locations are restricted for admin users/elevated applications.
Application data should be stored in the user application data files in the user profile (or the common application data).
If you really must write into the system folder, then you will need to ask the user for permission via UAC, either by using ShellExecute() with the runas verb to run another program, or COM elevation (if that's possible in Java)
Update
See Andrew's answer for a method to get the correct path in Java.
Put myFile.file in a sub-directory of user.home.
The sub-directory could be the package name of the class saving the file. E.G. if your main class is our.com.Main, store the file at ${user.home}/our/com/myFile.file. The reason to use a sub-directory is to help prevent another apps. myFile.file from overwriting or interfering with your own version.
To get the location of user.home, see:
System.getProperty("user.home");
The value of user.home here, is:
Name Value
user.home C:\Users\Andrew
This technique will work reliably for Mac. & *nix, as well as Windows.

Storing a file in the user's directory in cross-platform Java

I have a Java application that runs on Mac and Windows, directly off a CD/DVD with no installation. Now, I need to store a file containing per-user data (think favourites etc.) somewhere on the local file system, so that it can be written to.
So, where do you think is a good location for this file? I am thinking something like:
<USER_DOCUMENTS_AND_SETTINGS>/application data/myapp/favourites.db for windows
<USER_HOME_DIR>/.myapp/favourites.db for mac/nix
Thoughts?
And does anyone know the best way to determine these paths within Java?
System.getProperty("user.home")
As well as user.home, the following is useful for more temporary file storage:
System.getProperty("java.io.tmpdir")
Use the Java Preferences API, designed specifically to store user preferences values in a platform independent way we all like. Java will take care of saving them and retrieving from the file or other backing store depending on the OS.
These tutorials can get you going
You can consider storing the data at Roaming directory under Windows.

Search Entire Computer for a File Name in Java

Is there a way to search an entire computer for a String value representing a file name in Java? This would be without knowing the names of the drives on the computer.
You can iterate through the file system by looking at file names, getting the contents if it's a directory, etc. recursively.
If the sticking point is how to get the drives on the computer, look at the File.listRoots() function to get a list of the drive letters.
ETA:
To be absolutely safe, you'll want to include some limits on recursive processing. It's possible to have loops in the file system with symbolic links and such (especially in LINUX/UNIX, but third party tools can enable this in Windows as well).
To make sure you don't get into a loop when dealing with symbolic links, use the File.getCanonicalPath methods to get the "real" path for the directory and keep track of all visited canonical paths. You could also use getCanonicalFile and keep track of all the files, but that's probably not needed unless you really want to avoid the occasional instance where you'll process the same file twice.
You can use the File object to determine whether you are looking at a file or a directory:
File file = new File("/"); //root
Then as you are recursing (or iterating depending on your preference) you have a simple check:
if(tempFile.isDirectory())
//do recursive call on that directory
else
//perform check on file name
Also not forget exceptions in recursive processing. Some folders may not be accessible due to access right restrictions. Also, the Windows system folder "System Volume Information" cannot be entered in Windows Explorer, so I suppose it will throw an exception if you try to get inside programmaticaly.
You can use a recursive call through the entire file system: You can use the following methods of java.io.File:
listRoots(): list the root files (In Windows it's the drives, on Mac OS X and linux it's '/').
listFiles(): list the enclosing files of a directory

Categories

Resources