I have a java web service through which I upload images to a file server. I want to access these images from my java web app. How can I make the image files (and eventually other static files) available from this file server?
The only thing I could think of was to use Apache Http server as a proxy to my web app for these images, but that circumvents the security measures of the web app.
UPDATE:
Servlet container: Tomcat
Web app is on separate server from images.
Web service is on same server as images and has direct access to file system.
Both web app and service use spring security for authentication/authorization, I want to continue to use this security framework to for image access.
How are the files stored?
If security is a concern the best option might be to create a Servlet (or something similar) which will load up the image and serve it to the user, once it has checked their credentials.
How you load the image depends on exactly how they're stored, if you can access them via HTTP you can always open up a URLConnection to the file from the Servlet and serve it directly that way (i.e. using the Servlet as a sort of proxy server).
Without more details it's difficult to be specific.
I'm not sure if this will solve your problem, but it sounds like you should set up a context path that will map a URL to the path on your server. This can be done with tomcat's context files.
For a good explanation of the solution, check out a post on How to Program with Java
Sounds similar to Apache Hadoop.
Once image/file is requested, you have to make API call and pull the file out and do one of the following:
Store the temp file to the "temp" directory on web accessible server. You will need, some kind of cleaner/gc running in the background to clean those temp files. This is how Facebook does it with photos.
Instead of storing file on the server check the file type and set HTTP Content-type header to the appropriate file type. Image source will look like this <img src="getPicture.jsp?id=1234" />
Related
I'm developing local-only JSP application using Apache Tomcat server. I would like to put a promotion videos on my intro page, but I don't want to move them to webapp folder or anywhere else.
The promotion videos are located:
E:\data\videos\2018...
But writing a JSP/HTML like this wont launch the video, but however it works off-server (launching html from desktop for example, so the path may not be issue?)
<video src="file:///E:/data/videos/2018/promotion1.mp4" controls></video>
Local file links from remote resources are disabled by almost all browsers by default. There are certain possibilities to overcome that, e.g.:
http://kb.mozillazine.org/Links_to_local_pages_do_not_work
https://discourse.mozilla.org/t/opening-links-to-local-files-file/16449/2
To access your static media files from remote page you need to configure your Tomcat server as described here: http://teknosrc.com/access-local-files-static-content-images-videos-media-files-outside-web-application-system-apache-tomcat-server/
solution:
a.) make sure your server is on the same system where the media files are.
b.) If so, you have to create a folder (ex. media) in your application folder inside /src/main/webapp/ and have to put all media files inside a media folder. After that, you can surely access the media files through a server.
Ok, I'm a beginner so this maybe stupid but i afraid that clients can modify static resources (css/javascript files) on server if they can load them directly through URL path (Of course I have to put css/javascript files outside of WEB-INF folder).
If my hypothesis is wrong, could you give me links or quotes to help me expand my knowledge ? Thank you :)
When a user's browser requests resources from your server, they are performing a GET request. This request will not directly change any file on your server. The request will go through your web server and will be processed. In the case of the resources such as css/javascript files, the web server sees the user is requesting the file and sends the contents of the file back. There is no way the user can update the contents of those files on the server unless you write code on the server to allow them to update the files. If the user has direct access to the server via ssh or other protocol and has permissions on the folder that holds the resources, they would be able to change them.
The whole process is much more complex for going through the web server, but for brevity left out here. Here is a good article that explains what really happens when you go to an address in a browser:
https://medium.com/#maneesha.wijesinghe1/what-happens-when-you-type-an-url-in-the-browser-and-press-enter-bb0aa2449c1a
I am developing a jsf web application in which i have store some image files on my local computer lets say D:\images. The server running on this computer only. How can i access my local drive files on my web application.
I tried
<p:graphicImage value="D:\\Temp\tec0178.jpg">
or
`<p:graphicImage value="D:/Temp/tec0178.jpg">`
this not work for me.
If i place the images in my web application
<p:graphicImage value="resources/images/Male.png"/>
its working.
If you need to store it on a physical location on your server where your application is deployed, you could just work with basic I/0 operations in JAVA using File class.
Local drives are directly accessible.
http://docs.oracle.com/javase/tutorial/essential/io/
You could also explore Google Guava API for the same.
A simple example using Google Guava API is:
File imageFile = new File("D:\\images", imgFileName);
FileOutputStream outputStream = new FileOutputStream(newAttachment);
ByteStreams.copy(inputStream, outputStream);
Also, to access images over URL you'll have to add the directory as context. Check out the blog post to access image file as URL:
http://th1rty7.blogspot.in/2009/05/tomcat-is-often-considered-to-be-too.html
This is not exactly a JSF answer, but I like easy ideas: why don't you write a servlet called Images, for example, that receives as a parameter relative routes to your local directory? That servlet would know exactly where to look (you could have d:\Images stored in some properties) and would be as simple as:
<img src="http://yourserver/yourapp/Images?route=someImage.jpg"></img>
would lead to
ImagesServlet.java
...
// assume 'properties' is some way to access your application properties, be it in database,
// or .properties file, or whatever suits you best. In this case, it points to d:\Images
File imageFile = new File(properties.getRoute(), request.getParameter("route"));
InputStream is = new FileInputStream(imageFile);
ByteStreams.copy(inputStream, request.getOutputStream());
It is better to store your images in web application itself. you can call it like
<img src="\images\appimage.jpg">
We have a Java class that is supposed to fetch an HTML file and then read some content in it based on the id of certain divs and then return the content to a frontend which will then render it.
Now we have a set of HTML files on a common file system somewhere on the network. Multiple applications will access it. It is like a homegrown GUI help guide for our customer facing screens with a centralized storage.
We have managed to load the html file in 2 ways
Start an Apache web server and put all html files in htdocs. The calling Java class then makes an http call http://someIP:80/helpguide/userguide.html #firstname. This will fetch the help guide related to FirstName field on the screen. The Apache service has to be managed as it is accessed in Live but only accessible within our network.
Create a Shared directory and grant access to it to the Windows logon used to run the Windows service that runs Tomcat where the client facing web application is deployed. Then the Java client class uses new File("<file location>") to load the file and read its content. This works as well.
Basically we have 2 ways to load the html file. Now we are confused whether to use route 1 or 2?
The html files won't be that massive and will be of reasonable size. It may have inline css or youtube video links embedded in them.
Downside of (2) is if we want to include images later it won't work while it should work with (1).
However in terms of performance and efficiency how are teh 2 approaches different? (1) will open a Http socket connection over port 80 and get the html stream back. WIth (2) It will possibly use a File Inputstream to get the file on the server.
Hi all
Is there any methods to fetch the File List in the Web server from the Application Server using JAVA?
i am finding something like new file ("/webserver_context_root/folder/") method that using the relative path to get the web server's resources from the app server...
PS : The reverse proxy has been set between the web and application servers.
Any ideas?
The HTTP protocol provides no standard way to list a "folder". Indeed, the HTTP and URI / URL specs don't even recognize "folder" as a concept.
If the folder notion is meaningful for your website then there are two approaches that could work:
Many webservers can be configured to produce a listing (e.g. in HTML) for a URL that corresponds to a folder in the webservers content space. (This is usually turned off for security reasons.) You can "scrape" this HTML to extract the list of names of things in the folder.
You could implement a RESTful service to return a list of the "files" in a "folder" as (say) JSON or XML.
Note however, that both approaches will be specific to that website. They won't work for arbitrary websites. I'm also assuming that the "application server" accesses the "web server" using HTTP. If it can access it some other way, there may be other solutions.