J.Chandrasekhar Reddy
J.Chandrasekhar Reddy

Reputation: 101

Image is not displaying when trying to display it from out of my project (local disk)

I'm trying to display image in my jsp from my local disk inside a web project. My Code is:

<img src="C:/Documents and Settings/chandhu/HelloWorld0.png" width="80px" height="100px" style="margin-left: 20px;"/>

After I run my application the image is not getting displayed. In src attribute it is additionally adding some source as mentioned below:

<img src="http://localhost:8080/Ecm/C:/Documents and Settings/chandhu/HelloWorld0.png" width="80px" height="100px" style="margin-left: 20px;"/>

Can anyone tell me what I am doing incorrectly?

Upvotes: 0

Views: 144

Answers (2)

Asraful
Asraful

Reputation: 1290

You mentioned using jsp , therefor i think you are working with web application ,in your WEB-INF folder create a directory named image or as you needed and put your image there .

Now you can access that from code as :

String fullPath = servletContext.getRealPath("/WEB-INF/image/" + image_file_name+ ".jpg");
InputStream is = new FileInputStream(fullPath );

Or you may use :

InputStream stream = this.getClass().getResourceAsStream("/WEB-INF/image/image_file_name.jpg");  

to read stream directly .

related question may help you :

JPEG file in WEB-INF returned as null by ServletContext#getResource()

Cant retrieve an images from the WEB-INF folder ussing classLoader.getResourceAsStream()

Upvotes: 0

Niraj Patel
Niraj Patel

Reputation: 2208

You can't do this way.. It should be in reach of container. or you can write a servlet which will read disk file and make HTTPResponse

Upvotes: 2

Related Questions