Results 1 to 7 of 7
  1. #1
    allan123 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default Problem trying to display a .jpg image file on a web browser

    Hi guys, I am currently using an eclipse ganymede IDE to write and compile my code. I want to import and display a .jpg picture on a java server page. The picture displays successfully when I compile my code and run it on my eclipse web browser, but, the same does not happen when I type the same url on Internet explorer address bar. All the data gets displayed except my picture.

    I find this very weird because, eclipse uses an Internet Explorer browser.

    I even tried creating a .war file of my project and running it seperately on a tomcat server and the same thing happens. The picture file does not get displayed on the web browser.

    Can someone tell me what my problem could be? Do I have to enable any settion on Internet Explorer?
    Thanks!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    When you deploy it to tomcat can you find the picture in the correct place, presumably <app>/images/mymage.jpg?

  3. #3
    allan123 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your response.
    No, my images are stored on the local drive. eg) C:/Program files/ images/image.jpg
    Do I have to store it inside teh webapps folder?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Yep.
    In HTML the img tag uses a URL (either relative or full) to reference an image. It will not recognise something on your harddrive when run through a web server.

  5. #5
    allan123 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    ok. That makes sense. This is what I have done.

    String output = "C:/Users/Owner/Desktop/" + "images/Level2.jpg";
    <img src="<% out.print(output);%>" alt="Level2" />

    Could you tell me what I should do to get the url dynamically?
    Should I save the .jpg file inside 'webapps' folder? If yes, how do I get the url path of my application?

    Kindly advice. would really appreciate it.
    Thanks

  6. #6
    allan123 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    I just created a folder called images inside webapps and got the real path using
    getServletContext().getRealPath()

    This is thecode:

    String appPath = getServletContext().getRealPath("/images/")+"\\";
    String output = appPath + "Level1_oType.jpg";

    The jpg files are getting stored inside the images folder. When I retrieve them using image src, the image does not get displayed.

    <img src="<% out.print(output);%>" alt="Level1_oType.jpg" />

    Any suggestions?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Your path should simply be "images/" + filename.
    (Or possibly "/images/" + filename, I can never remember.)

    By using getRealPath() you have just added the whole path on your machine to the URL, which is incorrect...and takes you back to where you were before this change (essentially).

Similar Threads

  1. Sending image file over serversocket to browser client
    By maheshsk in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2009, 02:40 PM
  2. Sending image file over serversocket to browser client
    By maheshsk in forum Web Frameworks
    Replies: 1
    Last Post: 12-10-2009, 02:39 PM
  3. image problem when using any browser
    By ocean in forum New To Java
    Replies: 3
    Last Post: 09-17-2009, 10:29 PM
  4. [SOLVED] Display the text.log file in the browser
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 06-09-2009, 02:23 PM
  5. Display MS-Word file in browser
    By jazz2k8 in forum Advanced Java
    Replies: 6
    Last Post: 12-08-2008, 12:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •