how to access files on local file system from deployed web app? - java

I have a web app deployed on Tomcat on Windows 7. I have various files on the PC, local file system, that I want to access from my web app. How can I do this or is this possible?

Ok, looks like I found the solution.
Can just use the File() object with absolute paths and that does the trick.
I thought there would be some special requirements for web apps but it looks like there isn't

Related

Where to upload an image/file to make it appear on HTML?

Well,I'm working on an Eclipse Dynamic Web Project under Tomcat.
I'm trying to make a web application/site.In a jsp/html page,there is a form where a user can upload a photo.
I handle then this action from a servlet that has to store this image/file somewhere so as to make it possible the image appears whenever I want on the site.
Here is the problem.I started by storing it on my file system,(path in a database) but when I wanted to retrieve it the page didn't appear.
I guess the reason is here:
Why can't I do <img src="C:/localfile.jpg">?
Then,I tried to store the file in the eclipse project folder(WebContent/folder) where I've stored manually some images that do appear.
File folder=new File("/TED/res/img");
File file=new File(folder,fileName);
System.out.println(file.toPath());
Files.copy(fileContent, file.toPath());
But this exception happens:
java.nio.file.NoSuchFileException: /TED/res/img/2017-08-13-123524.jpg
It's one the line of files.copy command which means that
new File(folder,fileName) that I tried failed
What should I do? From what I've read,I understood that also saving file in the IDE's project folder is also wrong but what other choice do I have?
Ultimately, the project will be deployed to a server. As such, there are three distinct issues:
Uploaded user content location: content like images should be uploaded to a folder outside your web app (project). Images inside the web app (project) should be those that are necessary for the application and provided by the developer, not user-generated.
In Eclipse, during development and testing, you will want to serve these images through Tomcat. There are many ways to do this. Tomcat configuration is probably not the best for this - please read the answer and discussion here: Simplest way to serve static data from outside the application server in a Java web application
Once the application is deployed to the server, Tomcat will most likely run behind a Web server like Apache or Nginx. In this case, the external image folder and its contained files can be served directly by the Web server. Even if you implemented a servlet in (2) for local testing with Eclipse and this servlet is part of the code that is deployed, it will not be invoked as the URL will be intercepted by the Web server before it reaches Tomcat. For example, if your uploaded image folder is C:\images on your development environment, it can be served by the servlet using the technique in (2) as /images/*. When deployed to a server, the Web server can be configured to servet /images/* from /srv/content/images and this request will never reach Tomcat.

java tomcat application variables

I'm quite new to full stack java web app developement. I developed an app which deals with files uploads by the user to the server. My question is:
How to set, particularly the server-side storage path?
To be more exhaustive, I developed my project as a Java Web project in Netbeans (no Maven). I deployed the .warfile on the remote server and everything works fine, but, as I developed the app locally, I have a storage path set to c:\..., and I want it more like /opt/tomcat8/...
So, as the .war way is simple to operate, but also "blackboxed", how should I do ? Is that possible to specify parameters in an external file, for example ?

How can I externalize a PDF from a Java EE application?

I have a Java EE application, running under WebLogic 10.3.5 and Java 6.
I used to have a pdf help file, embedded in my war file, but I need to extract it from there and put it in an external directory (it can be in my same WebLogic domain directory).
I tried to put it in my WebLogic domain and then to < a href > it, but it seems that browsers have limitation and for security reason will not allow to download local file with a href or javascript.
This used to work only on a static HTML file saved on my computer but one the HTML page is deployed on the server, it seems not be possible.
Any idea how I can externalize my help.pdf file from my war file?
#limc is right
you should put this static file outside of Weblogic altogether as a file on an Apache web server
However, in Weblogic there is a feature of virtual directory mapping which allows you to declare a folder outside of the weblogic domain as a content store for any static stuff.
http://docs.oracle.com/cd/E11035_01/wls100/webapp/weblogic_xml.html#wp1039396
This entry goes in WEB-INF/weblogic.xml
<virtual-directory-mapping>
<local-path>c:/usr/mypdfs</local-path>
<url-pattern>/pdf/*</url-pattern>
</virtual-directory-mapping>
Although some application servers allow a Java EE app to reference a file outside the web container, in reality, your web app shouldn't have any knowledge about anything outside the web container, and as you have mentioned, it is indeed a huge security concern.
Depending on what you are trying to accomplish with this PDF file, if you merely want to expose this file on the web, do what #duffmo said and it will work fine. If you want the flexibility to modify this PDF file frequently without recreating the war file again and again, you may want to consider hosting this PDF file in some HTTP web server (Apache2, IIS, etc) and now you reference that link from your web app.
You need to put it at the root of your web context, in exactly the same place as HTML pages. Your web server will be able to find it there.

Is there a way to run and execute Java files using HTTP from another PC?

I have installed Java and developed on my laptop. Now I want to access those files from my PC using HTTP. Both machines are on the same network. I'd like to access the page as we do in PHP:
http:// and the path
How can I do it? I can see the files using this url: file://machine-name/login.html, but it's not opening in HTTP, replacing file from the url. Please help
Unlike PHP, in Java you can serve only JSP or Servlets. You will need to have an Application Server - like Tomcat, to deploy your web components. There are many different ways how you can package your web components. Typically all files (JSP, Servlets, Classes) etc. are packaged in a 'war' file. Thiw war file can be deployed to any Java compliant application servers.
Try this URL - Simple Tomcat application
Hope this helps.
Regards,
Sreehari.

Tomcat web apps location question

I am q java web apps developer.
I am using Eclipse+Tomcat.
Currently I am working on about 10 web apps.
Here is my problem:
My web apps are not all placed under a single folder, they are located in different places.
To test my changes I have to export my web apps into war files or to move the updated files manually every time so the Tomcat will recognize them.
This seems like a waste of time for me.
Is there a way to tell tomcat where my web apps located instead of copying files every time?
Are you using Eclipse Webtools?
If you're not, consider doing so. You will be able to configure Eclipse to launch Tomcat with your web applications, have them auto-reloaded, etc...
Short of that, you could still configure your Tomcat server to pick up your web applications from wherever you want by specifying appropriate document base in either server.xml or your webapp's context.xml
Here's a link to Server Tools documentation
Here comes a tutorial telling you how tomcat and eclipse are supposed to work together to forge a development environment.
http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html
Follow it and you will not be exporting the .war all the time.
You can do this easily with Tomcat. For each app you want run from somewhere else, you need to place a context fragment in $CATALINA_HOME/conf/Catalina/[host] directory. For example,
app1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/anywhere/app1" swallowOutput="true">
</Context>
The xml file name will be your context name.
You don't actually have to use Tomcat to test your apps... You can use the Jetty web server from within Eclipse, which is very comfortable.
You can try using Ant or Maven. It will solve this problems
I think you should load your application from context files rather than the server.xml. There is no need to load a application every time in the container. You can run your application from the place where it is.
Please use this link

Categories