In a java web application (struts) runs on tomcat, I need to upload a file (of any type) to a destination folder which will be taken as input parameter from the user. The destination can not only within the server directory but also anywhere in the system. Is there any api available for file upload or can this be acheived using java IO? Any suggestions with sample code will be appreciated.
Thanks.
Try changing "user.dir" property which has path of server directory. Here is sample code may be of some help
System.setProperty("user.dir", <destination path on your system>);
File f = new File(System.getProperty("user.dir"));
Use common-fileupload. There are more example for that here.
Related
I have an issue where I want to load a json file that is in the root folder of my app directory.So i did the simple
File file = new File("assetlinks.json");
when I run the application on my local server, the file is served correctly
but when I push online it the file is not found
it throws FileNotFoundException.
Please what am I missing here?
If it is the root of a web app, you need:
File file = new File(servletRequest
.getServletContext().getRealPath("/assetlinks.json"));
This turns an URL like path (/) relative to the web app's root into a file system File.
(BTW a subdirectory might be a better idea.)
After logging the current working directory, I discovered that it returned an empty string, this is because the app is bundled up in a container and to get resources those resources have to be explicitly added in the docker file, so simply adding the resource to docker solved the problem
ADD assetlinks.json assetlinks.json
After I did that I was able to get the file using
File file = new File("assetlinks.json");
I have an implementation which reads a file and uses java.net.URL to take a path to open connection to.
In production code, the link of file will be on FTP but for testing the logic I want to use a file locally which I can read using file protocol instead of ftp.
I am using Spring and changed the file path to something like below-
application-test.properties
#file location
enzyme.dat.ftp.link=file:///Users/username/project/src/test-integration/resources/input.dat
As you can see its a absolute path, which I need to make relative to my project. How can I do it?
I just need something like below-
#file location
enzyme.dat.ftp.link=file://${project.basedir}/src/test-integration/resources/input.dat
new URL("file:relative/path/to/file.txt")
The path is calculated starting from the current working dir of the application.
This answers the exact question, but actually, the recommendation to read the resource from classpath is better unless the file is really external to the project.
If the file you want to read is on the classpath then you could use something like the following:
new ClassPathResource("input.dat").getURL();
when trying to read the properties file, that way you can simply reference the file name ("input.dat") in your test properties. If the file is not on the classpath then you'll have to use a different mechanism.
I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? I have tried placing the files under source package, web folder, directly under the web-application.Please help.Thanks in advance
The simplest way to get the current directory of a java application is :
System.out.println(new File(".").getAbsolutePath());
Like that you can consider the given path as the root of your application.
Cheers,
Maxime.
Read the file as a resource. Put it somewhere in the src. For instance
src/resources/myresource.txt
Then you can just do
InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");
Note: if you are using maven, then you are more accustomed to something like this
src/main/resources/myresource.txt
With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path
InputStream is = getClass().getResourceAsStream("/myresource.txt");
I have a java console application inside of a jar file. It makes calls to a soap service via axis2. I am using this blog as the example. The exact configuration items I am adding are as follows:
<parameter name="Proxy">
<Configuration>
<ProxyHost>localhost</ProxyHost>
<ProxyPort>8888</ProxyPort>
</Configuration>
</parameter>
I tried putting that in an axis2.xml file in the root of my jar. I also edited C:\Program Files\Apache Software Foundation\axis2-1.5.4\conf\axis2.xml.
My AXIS2_HOME is set correctly:
set AXIS2_HOME
AXIS2_HOME=c:\Program Files\Apache Software Foundation\axis2-1.5.4
I verified the traffic is definitely being sent directly to the server via WireShark.
You are having this problem because JRE is unable to find the configuration file.
Yes, the configuration file should be placed outside the jar file because a program cannot read the configuration file inside a jar file (which is compressed).
The problem here is that you have to point your program to the configuration file correctly.
Relative filepaths are calculated from where the Java Runtime Environment was started.
(We should use relative filepaths because we want to avoid using absolute filepaths since not everyone will have the same system environment and thus absolute filepaths will not work in a different environment)
In this case, if you are unsure where your JRE is started from, making you unable to calculate your relative filepath, you can do:
File file = new File("");
System.out.println(file.getAbsolutePath());
This will help you find out where your JRE is started from. You should know that relative file paths are calculated from the directory where your JRE is started from.
For axis2, the working directory(where JRE is started from) should be the bin folder of your Apache Tomcat, while for your program, it will depend on where you made the call to the JRE to start the program from.
I would advise you to place your configuration file in a place where is easily accessible.
Say if you wanted to use AXIS2_HOME, and you place your configuration file in the AXIS2_HOME directory, you can do the following inside your jar program to find your configuration file:
String value = System.getenv("AXIS2_HOME"); // gets the AXIS2_HOME environment variable
File file = new File(value+"/"+axis2.xml);
I think you get the gist of what I am saying. Hope that helps! (:
how to get the full path of a file that is located at an ftp server using java (is it possible to get path using sinetfactory api)
You can't, as there is no convention for mapping ftp server trees to physical file names.