Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Images from Hard Drive

Status
Not open for further replies.

Dynapen

Programmer
Apr 20, 2000
245
US
I am trying to build a photo gallery into my site. I have all the images located in a folder under the site root

Site/Images/etc.....
Site/WEB-INF/classes/etc.....

The images folder,as shown above is at the same level as my WEB-INF. What I need to try to figure out how to do is determine the site relative path to the files so that I can display that in the HTML.

Code:
<img src="/site/images/image1.jpg"/>

Problem comes in with the fact that I am using java to read the images off my hard drive, and then pass the path info up to the JSP which will display it.The Java is determining where to start to get the files by using a props file I built that gives the absolute path to the files.
D:\Site\Images\etc.....

What happens is that the path that gets passed to the page becomes
Code:
<img src="D:\Site\Images\etc...."/>

This only works if you are viewing the pics from the same box they are hosted on. So i need a way to make the path that gets passed up relative to the Site folder.

Any ideas?

 
You need to place the images inside you webapp, and then link to them like so (like you posted) :

<img src="/site/images/image1.jpg"/>



--------------------------------------------------
Free Database Connection Pooling Software
 
I have them inside they webapp. They site in a images folder at the same level as the folders that contain teh JSp's.

But becuase I want to use Java to simply display all images that are in that folder (without knowing which ones are there) I have to be able to tell the java code how to make the path it returns (which is absolute normally , D:\site\images\etc....) look like the path I have listed in the <img src example.

Mainly, this is becuase I am going to support uploading of images, and allow users with access (me) to create new folders underneath the main images folder. But I want to be able to view all images under all folders at any time. Without hard coding the name of the images or folders.
 
Can you explain :

I want to use Java to simply display all images that are in that folder

Browsers display images for you without the use of Java ... perhaps you are missing telling us something ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Becuase it's for a photo gallery, I don't want to have to know all the folders or images taht exist. I want Java to read through the file structure and get all the images that exist. The pass up a collection of Strings that represent the file path to the JSP. That way the JSP can iterate through them and display the images. The problem is that the java class doesnt' know how to return the path relative to the site instead of the full OS path.

Basically, the java.io.File object's getPath() will return
D:\Site\Images\Test.jpg.
When it try to display it on the site I need that path to be relative to the site
How do I get the path switched from 1 way of representing it to another?
 
Then covert the locations :

Code:
String physicalPrefix = "D:/Site/Images/";
String sitePrefix = "[URL unfurl="true"]http://mysite.com/site/";[/URL]

String physicalImage = "D:/Site/Images/Test.jpg";
String siteImage = physicalImage.replaceAll(physicalPrefix, sitePrefix);
System.out.println(siteImage);

--------------------------------------------------
Free Database Connection Pooling Software
 
I guess that's what I am going to have to do. I was hoping that maybe the ServletContext might have a mechanism to do the translation for me, the same way the webserver does. Mainly becuase if I do a string replacement then I have a issue with the site translating things correctly if I come from a different address.

Mainly

vice
or

Make sense?

I mean the webserver knows how to translate the URL into a physical file address, isn't there any way to hook into that?
 
Well if you use your hosts file to control what IP points to, then you will never have any problems - just code it all for ...

Of course you can convert a File handle to a URL, but that would make no sense, as the URL would be relative to your machine only.

--------------------------------------------------
Free Database Connection Pooling Software
 
If you use servletContext.getResourcePaths("/Images"), it returns all the filename with path relative to the website root directory. This way, all paths are relative, and you don't have to worry about base path even if you move your web root directory. In a JSP page, you can do something like:

Code:
<%
Set imageFiles = session.getServletContext().getResourcePaths("/Images")
Iterator itr = imageFiles.iterator();
while(itr.hasNext()) {
  String imagePath = (String)itr.next();
  if (imagePath.endWiths("jpg")) {
%>
<img src="<%= request.getContextPath() + itr.next() %>"/>
<%
  }
}
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top