Unable to access Images inside a War file - java

I have a war file that I have deployed which contains an images folder in the WEB-APP directory. I tried accessing the images stored in the folder using
"localhost:8080/testapp/images/image1.jpg" but I am getting a 404 response. Can someone please help me with this?
Thanks in advance.

Your images directory needs to be a sibling of WEB-INF, not a child of it. Try this:
your-war-file.war
|-images/
| |-image1.jpg
| |-image2.jpg
| `-image3.jpg
|-WEB-INF/
| |-classes/
| |-lib/
| `-web.xml
|-index.html
`-404.html

Assuming you have your folder 'images' in the base directory for this web app (on the same level as 'META-INF' and 'WEB-INF' catalogs), the path seems correct. Maybe check that you haven't put 'images' folder inside 'WEB-INF' where it is not accessible? If that doesn't work, try accessing some other file inside your web app to check that it was deployed on server.

Clients may not directly access artifacts under WEB-INF.
Put them in a location directly accessible if you're not streaming them from an app endpoint.

how to read from path ?
use ("."+File.seperator+"some else") path of File for current path into your code or html file

Related

How access uploaded file in spring framework

I am new to Spring framework in spring site There is tutorial at https://spring.io/guides/gs/uploading-files/ that upload file to the root folder "upload-dir" (this folder is beside src root folder)
questions:
How can I access and show image in browser (or access it in thymeleaf by th:src="#{}" syntax) -
by browsing to localhost:8080/files/first.jpg because of controller it give me download link.
should I always upload file to folder that beside src folder for example I want to upload file to "src/main/resources/static/file" is it possible?
When accessing files in your code, Spring will (by default) assume that the src/main/resources is the parent directory. If you are planning on accessing the files that are uploaded, then I would use src/main/resources (or a subdirectory of this location) as the upload path. This way, you can simply access them in Thymeleaf as such:
Location: src/main/resources/picture.jpg
Thymeleaf: th:src="#{picture.jpg}"
Or if the file exists in a subdirectory:
Location: src/main/resources/somedir/picture.jpg
Thymeleaf: th:src="#{somedir/picture.jpg}"
If you are storing the file(s) elsewhere, then you can also access them using various prefixes like classpath or file, i.e.:
classpath:com/myapp/config.xml
See more about Resources in Spring here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html
Hope this helps!

Not able to access images within WEBContent/WEB-INF

My project hierarchy is like this.
ProjectName--> WebContent-->WEB-->INF-->ZUL-->XYZ-->images(Folder)+abc.zul
--> when I wants to access image within images folder in abc.zul(which is also inside XYZ fodler) images are not loaded.i am accessing images like this.
image="/images/app_icon.png"
I believe it should be WEB-INF instead of WEB->INF.
Any resource inside the WEB-INF can be accessed programmatically only.
You can put images folder out of the WEB-INF and under WebContent folder in order to access images as image="/images/app_icon.png".
Again it depends on your requirement and implementation.

Why doesn't tomcat see certain folders?

In my tomcat webapps directory I have various projects with servlets in them ..
say named test,beer etc
My question is irrespective of the web.xml or contents when i type localhost:8080/folderName I must be able to view the contents right? but it doesnt work for all folders , for some it says resource not found ,
Example my test folder opens in the browser and my beer folder doesnt , I restarted the browser and tomcat after adding or modifying folders , why can this happen , please explain
My dear friend there is a certain process/configuration/rule that tomcat follows in order to render any web content.It is not some magic happening.
Tomcat has a way of reading web deployments within the /webapp directory.
When we type some URL On the browser tomcat does the following:
Example.
URL : http://localhost:8080/foldername/xyz
Here tomcat takes the part of the URL after http://localhost:8080,that is foldername/xyz.
So here the first part which is foldername means name of the folder present in the /webapps folder.
So reading this tomcat goes inside that folder.Later tomcat is at the mercy of a file called web.xml.All mapping from /foldername/ i.e. /xyz in our case, onwards are present in web.xml.
In your case , if you type http://localhost:8080/foldername/ , tomcat knows that browser refers to webapps/foldername but does not know which resource html/jsp/servlet to forward the request so as to be able to generate a response.
Hence it gives a resource not found exception.
If you want to run the above URL (http://localhost:8080/foldername) then you need to configure a <welcome-file-list> tag in the web.xml file.
So for the folders which are working in your case with the above URL, just open their web.xml file and you shall find the <welcome-file-list> tag.
Actually this is what happened
I had a folder test which had some files (NO WEB-INF) , tomcat listed its contents on typing localhost:8080/test
My other folder beer had a WRONG WEB-INF web.xml configuration
So the following is clear to me now
Without a WEB-INF folder and web.xml tomcat will just list the contents of the directory but when you have a WEB-INF and web.xml and something is wrong in them it doesnt even list the directory contents .
Just my understanding of it .
Thanks!

cant read files from resources

Im trying to read files(xml, images) from src/main/resources. But it doesnt work.My resources folder is Source folder. For example, when i m trying to read log4j.xml tomcat looking for it in C:/bin... And i also cant read images from resources. I can read it only from webapp. I ve read, that tomcat automatically replace files from resources to webinf, but i think that it doesnt work in my case.
Please, help. I dont have any idea.
Even if the png is added to the WEB-INF folder it wouldn't be accessible from the JSP page in your case.
On runtime a JSP page renders into a servlet that return HTML code in the response. Then a browser will parse your tag <img src="/Pajero.png" ... /> and send a request onto YOUR_HOST/Pajero.png url to access the image and get the error code 404, because content of the WEB-INF folder is not accessible for a client side in a java web application.
Perhaps it will be better to place images on the webapp folder at your case.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("log4j.xml").getFile());
You can use something like this to get resources from classpath.
Regards

Tomcat - The requested resource () is not available, file mp3

I just want to finish a simple HTML 5 example.
<audio src="2.mp3" controls autoplay>
Your browser does not support the audio element.
</audio>
but it can't complete and this is the description from Tomcat:
"The requested resource (/DemoPlaying/2.mp3) is not available."
My folder structure:
It's obvious that your mp3 file is not available at the URL you're trying to request it. Correct your 'src' to point to the correct url.
I think you must remember:
1) If your app is wellformed, then the typical structure is like:
http://myserver:8080/myapp
2) Then your file must be published under the contexto of your app
http://myserver:8080/myapp/mp3file
3) So, in development time your structure must be:
Webcontent
|
|--index.jsp
|--mymp3.mp3
|--WEB-INF
4) And war file must content the same of Webcontent (or root folder of your app)
Only in that way your file will be detectable by the server, and dont forget to registrer the extension on Tomcat Server

Categories

Resources