Issues with getting realpath while working with Eclipse Indigo - java

I am developing a web application.
My project is stored in
F:\Pro1\OHCMS
Now i want to upload a video in
F:\Pro1\OHCMS\Webcontent\Video
folder.
When i write
filePath=getServletContext().getRealPath("/Video")+"\\";
It is returning file path as
F:\Pro1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\OHCMS
How can i get the real path of my actual Webcontent folder ?

Your application is being run by your IDE's default application server. That is, your WebContent folder is actually copied to somewhere else (check out your build script to know where exactly is it copied). And after that, all your data is published to server.

Related

Get absolute path to project directory in application.properties

I am writing a Quarkus application which reads data over http. In my application.properties file, I have this line:
my.resource=http://path/to/file
Every time I run the app, it has to download the file so I created a smaller version of the file locally for developing purpose. The problem is that I don't know how to put it in the properties file.
Ideally, I want something like this:
my.resource=http://path/to/file
%dev.my.resource=file://${project-dir}/sample_data/file
And I have to use the absolute path because I used new URI(resource).toURL() method which requires an absolute URI.
Thanks in advance.
Application properties is something that is used when your application is deployed to adopt your application to the target environment, does the user of the deployed application know anything about project directory? Project directory is something that makes sense when you are developing your application. having said that using project directory in that file does not make sense at all.

unable to find servlet(.java) files converted from jsp

I am using eclipse luna and tomcat version 7. I have written few jsp files and executing them on tomcat easily. I have read that jsps are converted into servlets at run time and you can locate them in tomcat/work/catalina/localhost/project name and further. My project name is quizilla and there exist a folder named quizilla-1.0-SNAPSHOT, but this folder is empty. What is the reason and where can i find those .java files. I have attached the screen shot of the folder as wellAs I am in right directory in search of my java files, but the folder is empty. So what should i do
You are using Tomcat from Eclipse, so the work directory is:
projectworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
or something like that (if you haven't changed the Server configuration via Eclipse).
Add
<%=getClass().getResource(getClass().getSimpleName() + ".class")%>
to one of your JSP pages to detect where Tomcat has generated the servlet.

user.dir is incorrect while using tomcat web app in eclipse

I have a configuration project that few projects are using it.
All of my project under the same workingspace.
workingspace/configuration
workingspace/webapp1
workingspace/mongoDB
workingspace/model
mongoDB and the model project are using the configuration jar project and able to read the xml files using relative path ../configuration/conf/....xml
when using the
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
inside both of them, I'm getting the correct path (workspace)
while trying to do it from the web app servlet i'm getting the folder of eclipse.exe that causing some problems.
How can i fix the user.dir defalte path for the web app?
At runtime, e.g. when you're not running from inside eclipse, you probably want to work in a defined directory as well - I'd suggest to explicitly configure a specific directory. When you're running within an appserver, you might run as an unpriviledged user that doesn't have a home directory writeable at all (when the account is properly administered with minimal permissions). This differs from applications that are launched ad hoc. In fact, you probably can't assume that your application server runs as the same user as your standalone apps do.
Another alternative is to utilize the system's temp directory - if these are truly temporary files. This could be assumed writeable, or complaints if it isn't do make sense.

Spring MultipartFile upload file location

I am using Spring framework MultipartFile to upload files using my application to the server, though whenever I do any 'Project > Clean' from Eclipse STS, all files I've uploaded earlier get deleted automatically.
I am saving all files uploaded to the following path:
String rootPath = request.getSession().getServletContext().getRealPath("contentrepository");
Which in real path get translated to:
C:\springsource\apache-tomcat-7.0.40\webapps\myapp\contentrepository\
so can someone please tell me what I am doing wrong here to cause those files to get deleted each time I do a Porject > Clean? Is it that STS clean command empty the webapps folder content? Shall I change the upload files folder location to another locations outside the apache folder totally?
Thanks or your time
Spring Multipart upload has nothing to do with your problem!
Whenever you clean your project, all deployed directories will be cleaned. Similarly, when you deploy your project, complied files will be deployed to server.
When you upload a file, it is save in your deployed location, however on cleaning all the data is lost.
If you want to persist file during your testing i.e want to have files even if you clean your project:
- Store uploaded file to some other location.
OR - Take backup of uploaded files before you clean your project and copy them to deployed folder.
You might like to view this question, for what exactly 'clean' does.

Locating created file with Java EE and Tomcat

I have created a dynamic web project, and use Apache Tomcat as a server.
In my servlet I'm creating a text file and want to reuse that in a JSP. However they are by default created in the installation folder of Eclipse when I do something as simple as the following:
File f = new file("test.txt").
I don't know why this happens. Is there a way to create the file in the WebContent directory as I want to make that file available for download in my JSP.
Java has a concept of the "current directory". When you start an application via Eclipse, this may indeed point to your installation directory. If you don't specify any path, a file will be created in this current directory. Hence the reason why your test.txt ends up there.
The WebContent directory is a something that is specific to Eclipse. Your code should not depend on putting anything there. You only start your application via Eclipse when you're developing it, not when you're deploying it to a live server.
The content of this directory will become the root of your .war, which is a well known location independent of how you start & deploy you app, BUT you still cannot depend on writing anything to this location at run-time. You might deploy your application as a packaged .war (likely for live deployments) or you may deploy your application unpackaged but then your application server may simply not pick up any changes done at run-time.
What you can do if you are sure your application only runs on a single server is writing the files to a well known location on your file system, such as /tmp, or /var/yourapp/files, etc. The code serving up those files can then pick them up from that location.
If you want to play it 100% safe according to the Java EE rules, you'd store your files on something like an FTP server that has a configurable address. Technically your war could be shipped between nodes on a cluster and requests could end up going to different machines, so depending on a local filesystem wouldn't work then.
Executing this statement this.getServletContext().getRealPath (""), you'll obtain the path where Tomcat WebServer is pointing at at runtime. You could add a folder "MyFolder" and call this statement:
new File(this.getServletContext().getRealPath ("") + "/MyFolder/test.txt");
Anyway, the default path looks something like:
...\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\<NameOfYourProject>
Note that when you create a new file, it won't appear in your immediate workspace (check the .metadata path), unless you change the runtime location tomcat should point at.

Categories