Image not showing in src tag - java

Can anybody tell me how do i give absolute path of the img tag's src attribute?
The following doesn't work
<img alt="NO IMAGE" src="/home/administrator/tiger-info0[1].gif"/>
I am working On Ubuntu and i am very sure that image exists on this path.

This is probably happening because the image is located outside the web server's document root.
Your web server will not be able to serve anything from outside the document root. One possible workaround is to use a scripting language that has access to the file system, and route the images through the script. For example, you may want to check out the following implementation in php:
Serving Images Outside Document Root Via PHP
You can also create a symbolic link of /home/administrator/ into the document root:
ln -s /www/yoursite /home/administrator

hmm why don't you copy the image to your web directory and give it the relative path? you server (apache?) may not be able to access the file to serve the browser.

if you are making a local html page you can use that path but if you are creating a website you have to use the absolute path to the document root. And make sure the image path is correct (use firebug)

Give your path correctly with domain or use ../ or ./ is for to represent correct relative path.

You cannot access files that are not in your document root. Get Java application server to not delete your folder. You can probably do this by having one folder into which your users can upload files, and add that folder to your project. You can let users create subfolders inside that main folder, and since the main folder is a part of your project, cleaning the build will not automatically delete it or its subfolders.

Related

Downloading a local file

Files is stored in this location.
D:/uploads/component.png
This is the HTML code to download a file.
<a target="_blank" href="../{{att.filePath}}"><strong>{{att.fileName}}</strong></a>
But when I click on the link it opens up as in this path, which is wrong as it is not in the server.
http://localhost:9190/D:/uploads/component.png
How can I see the file in web browser, what am I doing wrong here.
Though you are not showing your variables' contents, it seems your variable contains the absolute path for the file. Therefore you don't need to add relative parts (e.g. ../) to the path.
Also, if you want to host a file for downloading, you must make it available inside the hosted directory (document root). You can achieve this in multiple ways:
A) Simply copy the file/directory into the document root. Then you can also use relative links if you wish (you have to change the variable!).
For example, you may have a structure like this:
public_html/
uploads/
component.png
index.html
Then you can link the file in index.html using the absolute path /uploads/component.png, or using the relative path uploads/component.png.
B) Create a symbolic link to the file/directory inside the document root.
For example if you create a symbolic named uploads in your document root for your D:/uploads/ directory, you will have this structure:
public_html/
uploads/ -> D:/uploads/
index.html
This way you can still have the file physically at D:/uploads/component.png but it will be available also in public_html/uploads/ and you can use the same paths as in method A).
C) If you are developing a webapplication (it seems so, as you tagged Java and Tomcat), there is another option. You can define a controller method which maps requests like /uploads/* and implement it so it will read the file specified by the URI from your D:/uploads directory.

Java Web Application. Problems in locating images

I'm making a java web app, and I want it to display an image. However, it doesn´t find my image.
I've made a folder in /src/main/resources/images
Also, in the .jsp file, I´ve tried with the following sentences.
<img src="/src/main/resources/images/Head.png"> </img>
<img src="< c:url value='/src/main/resources/images/Head.png'/>"> </img>
Is there anything bad I'm doing?
Thanks
Edit:
The path of mi .jsp file is /src/main/webapp/WEB-INF/jsp/welcome.jsp
You can find the web app code in https://github.com/Santi-7/hello
Your app is a Spring Boot app. I think that you can also try to use the facilities provided by Spring Boot for serving static content. Anyway, you are doing it right now because you are using webjars for css and js libs!!! Be consistent with the tech that you are using.
The structure of a .war file is as follows:
/
/WEB-INF
/lib
/classes
/META-INF
Now, your application has the following structure (I assume, given the folder structure, you are using Maven)
/
/src
/main
/java
/resources
/webapp
Now, the Maven war plugin will copy everything in the classpath to /WEB-INF/classes during compilation - this is /src/main/java and /src/main/resources by default.
The crux of the matter is that nothing under /WEB-INF or /META-INF can be accessed by requests - this is for security as otherwise someone could simply download /WEB-INF/web.xml for example.
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
So if you place Head.png into /src/main/webapp/images then in the JSP you would use:
<c:url value='/images/Head.png'/>
In short, you need to read up on how the directory structure of a .war works and how that relates to your code.
The path to the image must be relative to the path to the .jsp file.
Because the path to your image is: /src/main/resources/images/Head.png, and the path to your jsp file is: /src/main/webapp/WEB-INF/jsp/welcome.jsp, in your image tag you need to write:
<img src="../../../resources/images/Head.png" />
The ../../../ is for getting out from the jsp folder to the main folder, and the resources/images/Head.png is the path from the main folder to the image.
Thanks everybody, i could resolve my problem.
Changes I made:
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
Now, my images are in /src/main/webapp/images.
The path to the image must be relative to the path to the .jsp file.
Now, the sentence of my .jsp file is
<img src="images/Head.png" />
Edit [1]:
¡ I made a mistake. The path to the image is relative to the /webapp classpath !

Current directory of a web app in netbeans

I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? I have tried placing the files under source package, web folder, directly under the web-application.Please help.Thanks in advance
The simplest way to get the current directory of a java application is :
System.out.println(new File(".").getAbsolutePath());
Like that you can consider the given path as the root of your application.
Cheers,
Maxime.
Read the file as a resource. Put it somewhere in the src. For instance
src/resources/myresource.txt
Then you can just do
InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");
Note: if you are using maven, then you are more accustomed to something like this
src/main/resources/myresource.txt
With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path
InputStream is = getClass().getResourceAsStream("/myresource.txt");

Avoiding complete path of folder and Image not been shown in browser

I am making a web application using jsp and servlets .But am facing two problem that i have no ides how to remove :
Problem 1 : I am creating new folders in my WEB-INF folder .But what i want is that instead of giving full paths .I just provide relevant path Like :
File tempfilesstore = new File("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\RetrievedFiles\\"+fileid+"-"+personname);
if(!tempfilesstore.exists())
tempfilesstore.mkdirs();
Can this full path be avoided as only path from web folder of the application is required.
Problem 2 : I keep a image in this folder by performing some operation on original image being browsed by the client on browser.
Now when i see the image in folder then it is present their But if i try to see the same image in browser it does not display the image .When i refresh my page for 3-4 times than sometimes it get displayed and sometimes after manually opening it by going to specified location.What can be reason for it ?Please help.
Here is how am trying to get image on browser :
<img src="RetrievedFiles/<%=path%>/<%=sharedfilee%>" alt="Image Preview Not Availablee" width="300" height="300" />
Here ,
String path=presentfileid+"-"+personname;
String sharedfilee=rs.getString("FILE_NAME");
First of all, in a JEE point of view, all files within the WEB-INF folder are not meant to be accessed by anyone but your server. It means that images, CSS files, javascript files, etc. in this directory will not be rendered by your web browser. Your JEE server will prevent that to happen.
So, in order to access files from your web browser, you need to put them outside the WEB-INF folder (at the same level, in a "images" folder, for instance).
For your first problem : Yes, you can use relative paths to instantiate files, using your classLoader.
this.getClass().getClassLoader().getResource("resourcePath")
or
this.getClass().getClassLoader().getResourceAsStream("resourcePath")
depending on your needs. The first one returns a URI (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)) whereas the second returns an InputStream (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)) that you can use with a FileInputStream.
The ressources are located in the classes folder, and the path is relative to the class your get the resource from. For example, you can use "/myImage.png" as a path to get the image at the root level.
Putting all your resources files in the "classes" folder (within subfolder if you want to) is a good architecture design.
For your second problem, you have mainly 2 solutions :
if the image is rendered without any transformation, put it in a folder outside WEB-INF (see the beginning of my comment), and it will be visible from outside. In you JSP, you can access it like that :
request.getContextPath() + "/" + sharedfile
if the image needs a transformation, use a servlet instead
I hope that helps you.
Regards,
Alexandre FILLATRE

How to get file system path of the context root of any application

I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.
Then I am appending some path as request.getContextPath() + "abc" and create folder.
Then its creating folder in D:// instead of my webapplication folder.
Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.
You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.
Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.

Categories