How to crete an image File using relative path in java?
I am using eclipse galelio to develop a web app. I need to read a file from user local directory and place it somewhere in my application so that user can view it at a ater point as desired.
for this i am doing this :
f=new File("images/myfile.txt");
if(!f.exists()){
f.createNewFile();
}
but this file is being created in C:\Documents and Settings\MB\images\myfile.txt
not in applic开发者_高级运维ation (where I want) .
How to get this in application ?
It is a bad habit to store user-created content in the web application. Typically web applications are deployed as wars, and extracting the war (if it happens) is up to the container. You don't want to write in a directory "owned" by the container.
Typically you assign a place on the filesystem (or in the database) where you put user created content.
You can configure such a place using the context (in Tomcat context.xml), you can retrieve that property in a servlet (getServletContext), and use that configuration in your application.
That would also give the system administrator the possibility to put the data on a place which gets backed up frequently.
Even if you don't have such an organization the habit to configure file system places is not bad.
The base directory for a relative file path is the current directory. For standalone Java applications, this is the directory from which you started Java (if you don't give a -Duser.dir=...
option). What it's in your Web-App-Container, is completely unspecified, I think.
Do you want to save the file on the server or the client?
- If client, you'll have to use a applet or Webstart application, and then would probably use a FileChooser to let the user determine the directory, or better use the
javax.jnlp
api (FileOpenService, FileSaveService or PersistenceService). - If server, then you'll use the mechanisms of your container to get the right path (or directly store/retrieve the data). (I don't know any details about this, sorry.)
精彩评论