adf | how to display an image outside application root? - image

I'm trying to display an image which location was stored in DB. This is
My question is, how can I display this image using af:image, where source is the specific path outside application root?

Write a servlet that reads the image from the database and serves it.
In af:image - set the source to this servlet along with whatever key you wish to use (e.g., /myServlet?photoId=AAAAAAA)
Inside the servlet, make sure that you set the appropriate response headers (like expires, content-type, etc.)
Hope that helps.

if this image accessible by http then you can just write full path to this image (or relative with some "../../" to get to the right upper level).
IDE will complain, but it will work without any problems.

If the image location is an external URL, you can do:
<af:image source="http://some-address.com/someImage.jpg" id="i1" shortDesc="Hi"/>
Possibly you will get a warning in JDeveloper (depends on the version you're using), but if the URL exists and is not forbidden, it will work.

Related

Can I serve files stored in Google Cloud Storage via a http.FileServer in golang?

I have developed a small web application that runs a web server in golang.
Each user can login, view the list of their docs (previously uploaded) and click on an item to view an html page that shows some fields of the document plus an tag with a src attribute
The src attribute includes an url like "mydocuments/download/123-456-789.pdf"
On the server side I handle the URL ("mydocuments/download/*") via an http Handler
mymux.HandleFunc(pat.Get("/mydocuments/download/:docname"), DocDownloadHandler)
where:
I check that the user has the rights to view the document in the url
Then I create a fileserver that obviously re-maps the url to the real path of the folder where the files are stored on the filesystem of the server
fileServer := http.StripPrefix("/mydocs/download/",http.FileServer(http.Dir("/the-real-path-to-documents-folder/user-specific-folder/)))
and of course I serve the files
fileServer.ServeHTTP(w, r)
IMPORTANT: the directory where the documents are stored is not the static-files directory I sue for the website but a directory where all files end after being uploaded by users.
My QUESTION
As I am trying to convert the code for it to work also on Google Cloud, I am trying to change the code so that files are stored in a bucket (or, better in "sub-directories" -as they do not properly exist- of a bucket).
How can I modify the code so to map the real document url as available via the cloud storage bucket?
Can I still use the http.FileServer technique above (if so what should I use instead of http.Dir to map the bucket "sub-folder" path where the documents are stored)?
I hope I was enough clear to explain my issue...
I apologise in advance for any unclear point...
Some options are:
Give the user direct access to the resource using a signed URL.
Write code to proxy the request to GCS.
Use http.FS with an fs.FS backed by GCS.
It's possible that a fs.FS for GCS already exists, but you may need to write one.
You can use http.FileSystem since it is an interface and can be implemented however you like.

access to a image in the view/admin/sub_folder

I have a subfolder in "views/admin/my_folder".
Inside of this, is a image "test.jpg".
If I use the link "views/admin/my_folder/test.jpg" to display it I receive a error 403 - access denied.
The link will be generated from a part of the controller in this way
APP_BASE_URL.'application/views/admin/my_folder/test.jpg
So how I can display this image (and use it inside the other php-files from the "my_folder" and/or in the controller and model) - or in which file(s) I have to do something else so that I can have access to this image?
After a long term for looking for a better way, i have to accept that i shoul move the folder outside the application - this is much more easier and more secure as do a lot of change at the router and other settings...

How to map filesystem path to http url in spring

I'm working on a spring web mvc project which allows users to upload files. I am saving these uploaded files out of application context so that they persist across deployments. Saving file is working fine. I want to know the best way to convert the file system path to HTTP url so that it can be saved in the database and also used in HTML resource like etc.
Thanks in advance.
As you want to access a file in the filesystem like a static resource using spring mvc, the answer (taken from here) is to serve the static resources adding an entry like in your servlet context:
Example:
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/the/resource/folder/" />
In this case you store all the files in the same path in the same server using only one resource, then you could only store in the db the filename.
In your html tag you need to use a relative url (being aware of your context, so you could access your file like http://yourhost:port/context/resource/yourfile).
In case you want to store files in a different server then you should add another resource origin (but it must be available as a file system path to the other server), so in that case it would make sense to store in the database a value like "resourcename/filename".

Servlet send image from server and save in client

I'm new and just developing on J2EE.
I am modifying an existing application (an OpenSource project).
I need to save an image on a client sent by the server, but I do not know how.
This activity must be done in a transparent manner without affecting the existing operation of the application.
From the tests done I get this error:
java.lang.IllegalStateException: getWriter () has Already Been Called for this response.
How should carry out this task, according to your own opinion?
How do I save on the client, locally, the image?
Update:
Thanks for the answers.
My problem is that:
the image is generated on the server, but not for direct client request (there is no link to click on web page), the picture is composed using other services on the Internet.
reconstruct the image on the server.
This image must be sent to the client to be saved locally.
so I'd like it to appear a window where you assign the destination image
plus I'd like the rest of the application were not affected by this activity.
The application is yet on production.
Thank you very much for your response.
From the tests done I get this error: java.lang.IllegalStateException: getWriter () has Already Been Called for this response.
In other words, you were trying to mix the binary data of the image with the character data of the HTML output, or you were trying to do this in a JSP instead of a Servlet. This is indeed not going to work. You need to send either the image or the HTML page exclusively in response to fully separate requests.
In your JSP/HTML page just have a link to the image, like so:
click to download image
Then, in a servlet listening on an url-pattern of /imageservlet/*, you just get the image as InputStream from some datasource (e.g. from local disk file system as FileInputStream) and then write it to the OutputStream of the response the usual Java IO way.
You only need to set at least the Content-Disposition response header to attachment to make sure that the client get a Save As popup dialogue, else it will be displayed straight in the browser. Setting the Content-Type and Content-Length are also important so that the browser knows what the server is sending and can predict how long the download may take.
response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
You can find complete basic servlet example in this article.
Note: you cannot control where the client would save the image, this would be a security hole. This way websites would be able to write malicious files on client's disk unaskingly.
Update: as per your update, there are two options:
You need to let the client itself fire two HTTP requests (I've answered this in your subsequent question)
Create a client side application which does all the task directly at the client side and then embed this in your webpage, for example a Java Applet. With an applet you have full control over the client environment. You can execute almost all Java code you'd like to execute and you can write files to disk directly without asking client for the location to save. You only need to sign the applet by a 3rd party company or the client needs to confirm a security warning before running.
Its up to the browser how all types of output are handled. Web pages are given a content type of html which the browser understands and ends up rendering ass a page that we can see. Images are given content type of image/jpeg etc which are rendered as images when in a page etc. To force a download prompt one needs to use a content type of a binary file rather than that of an image so the browser forces the download rather than shows the image. To ensure this use something like "application/octetstream"... i cant recall exactly but its easy to google for.

How to use ExternalDirectory Resource

How to use External Directory to Store Images.
And how i access that images thru my Web application ?
I am using Jboss as an application Server.
Web application is in Java,Jsp.
Presently images stored in WAR file.
After google i got the solution
C:\jboss-4.0.0\server\default\deploy\jbossweb-tomcat55.sar\server.xml
Then restart the server and access the
http://localhost:8080/contextname/images
Please provide comments
I've answered similar question before: Simplest way to serve static data from outside the application server in a Java web application
To summarize there are basically two ways:
Add a new Context to the server.xml denoting the absolute location where the images are.
Create a Servlet which gets an InputStream of the image using FileInputStream and writes it the usual Java IO way to the OutputStream of the response, along with at least Content-Type, Content-Length and Content-Disposition headers.
See the link for more detailed answers and code examples.

Resources