Read directory contents and output in in JSP page

Posted on 2006-05-23
Last Modified: 2012-05-05
Hi all,

I have several directories of static images and swf files. I want to build a JSP that reads the files, by file type, and renders the images/swf's on the output page. I'd also like to sort by date or file name. Make sense?

I can do this in Perl pretty easily like this:
opendir(IMD, $dirtoget) || die("Cannot open directory");

Then dump into an array:
@thefiles= readdir(IMD);

From there I could output however I wanted. Not sure how to do this in JSP. Help please... :)
Question by:mookie13
    LVL 2

    Expert Comment

    Try something like this...

    String the_path = "/home/www/site/";
    File theDir = new File(the_path);
    String[] theDirectoryFiles = theFile.list();

    for(int i=0; i<theDirectoryFiles.length; i++) {
        File theFile = new File(thePath + "//" + theDirectoryFiles[i]);
        //this file can then be printed out or whatever.

    LVL 27

    Accepted Solution

    I had the pieces to this so I put it together. This should give you a start.    rrz

    <%@ page import="*,java.util.*" %>
    public class FileComparator implements Comparator{
         public int compare( Object a , Object b ){
           File aFile = new File((String)a);
           File bFile = new File((String)b);
           if(bFile.lastModified() > aFile.lastModified()) return 1;
             else return -1;
        FilenameFilter gifFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".gif");
        FilenameFilter jpgFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jpg");
        //assuming your home directory is in your web app's root directory
        File dir = new File(application.getRealPath("/home/www/site/"));
        String[] gifFiles = dir.list(gifFilter);
        String[] jpgFiles = dir.list(jpgFilter);
        Arrays.sort(gifFiles, new FileComparator());
        Arrays.sort(jpgFiles, new FileComparator());
        for(int i =0; i< gifFiles.length;i++){
                    out.print("<img src=\"" + request.getContextPath()
                              + "/home/www/site/" + gifFiles[i] + "\" />");
        for(int i =0; i< jpgFiles.length;i++){
                    out.print("<img src=\"" + request.getContextPath()
                              + "/home/www/site/" + jpgFiles[i] + "\" />");

    Author Comment

    This is great!!! I was just now able to get back to this project. Appreciate your help!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now