newtt13 0 Newbie Poster

Currently, I have a jsp that lists all files in a directory and subdirectories in a web page. The problem that I encounter is that when I navigate to files 2 or 3 subdirectories deep, the resulting web link adds erroneous spaces to my address. These spaces are making the urls unreachable. From what I have been able to gather, this jsp is the cuprit. I have been told that the script was not written completely to support navigating to files in subdirectories 3 deep or more. Is there any possibility to continue the jsp to correctly link files in subdirectories 6-7 directories deep?Any idea as to what I need to add to support what I need? I do not have the foggiest idea on how to use jsp. Any assistance that can be offered would be greatly appreciated.

<!-- Header -->
<html>
<head>
<title>File List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV="expires" CONTENT="0">
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
</head>
<body bgcolor="#FFFFFF" background="images/sumi2.jpg">

<%-- Error display --%>
<% if ( category == null ) { %>
  Error: Category not received.
<% } else if ( cd == null ) {%>
  Error: Category not found.
<% } else { %>
<%-- File list display --%>
<%

  String subDir = request.getParameter("subDir");
  if ( subDir != null ) {
    if ( subDir.indexOf("..") > -1 ) {
      System.out.println("Invalid subdir: " + subDir);
      subDir = null;
    }
  }

  String urlPath = cd.urlPath;
  String realPath = cd.realPath;

  // Look in selected subdirectory
  if ( subDir != null ) {
    if ( urlPath.endsWith("/") )
      urlPath += encode(subDir);
    else 
      urlPath += "/" + encode(subDir);
    if ( urlPath.endsWith("/") )
      realPath += subDir;
    else
      realPath += "/" + subDir;
  }

  File selectedDir = new File(realPath);
  String[] dirItems = selectedDir.list();
  if ( dirItems != null && dirItems.length > 0 )
    ArraySort.sort(dirItems);
%>

  <font size="-1">
  <b><img src="images/dir.gif"><%=category%>
  <% if ( subDir != null ) { %> /<%=subDir%> <%}%>
  </b><br>
  <a href="#" onClick="javascipt:window.location.reload(true)">(Refresh)</a><br><br>
  <table border=0 cellspacing=1 cellpadding=0>
  <% if ( subDir != null ) { %>
    <tr><td nowrap>
    <img src="images/dir.gif"></td>
    <td nowrap><a href="file_list.jsp?category=<%=URLEncoder.encode(cd.name)%><% if ( subDir.lastIndexOf("/") > -1 ) { %>&subDir=<%=URLEncoder.encode(subDir.substring(0, subDir.lastIndexOf("/")))%>
    <% } %>
    ">(Up Level)</a></td></tr>
  <% } %>

  <%
    int dirItemsLength = 0;
    if ( dirItems != null )
      dirItemsLength = dirItems.length;
    for ( int loop = 0; loop < dirItemsLength; loop++ )
    {
      File current = new File(realPath, dirItems[loop]);
      // Only list files and directories
      if ( !current.isFile() && !current.isDirectory() )
        continue;
    %>
      <tr><td nowrap>
    <%
      // Display directory link
      if ( current.isDirectory() ) {
        String cSubDir = "";
        if ( subDir != null )
          cSubDir = subDir + "/";
        %>
          <img src="images/cdir.gif"></td>
          <td nowrap>
          <a href="file_list.jsp?category=<%=URLEncoder.encode(cd.name)%>&subDir=<%=URLEncoder.encode(cSubDir + current.getName())%>">
          <%=current.getName()%></a></td></tr>
        <%
        continue;
      }

      String imageName = "doc.gif";
      if ( current.getName().endsWith(".bmp")
        || current.getName().endsWith(".gif")
        || current.getName().endsWith(".jpg") )
        imageName = "picture.gif";
      %>
        <img src="images/<%=imageName%>">
        </td><td nowrap>
      <%
      char extraSep = '/';
      if ( urlPath.charAt(urlPath.length()-1) != extraSep )
        urlPath += "/";
      %>
        <a href="<%=urlPath%><%=URLEncoder.encode(current.getName())%>" target="<%=target%>" >
      <%=current.getName()%></a>
        </td><td nowrap>
      <%
        // Show file size
        long fileLength = current.length();
        String units = "";
        if ( fileLength < 1024 )
          units = "B";
        else if ( fileLength < 1024000 ) {
          fileLength = fileLength / 1024;
          units = "KB";
        }
        else {
          fileLength = fileLength / 1024000;
          units = "MB";
        }
        %>
          <%=fileLength%><%=units%><br>
          </td></tr>
        <%
    }
%>

  <% if ( dirItemsLength == 0 ) { %>
    <tr><td></td><td nowrap>Directory Empty</td></tr>
  <% } %>
  </table></font>
<% } %>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.