jqueryFileTree.jsp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <%@ page
  2. import="java.io.File,java.io.FilenameFilter,java.util.Arrays"%>
  3. <%
  4. /**
  5. * jQuery File Tree JSP Connector
  6. * Version 1.0
  7. * Copyright 2008 Joshua Gould
  8. * 21 April 2008
  9. */
  10. String dir = request.getParameter("dir");
  11. if (dir == null) {
  12. return;
  13. }
  14. if (dir.charAt(dir.length()-1) == '\\') {
  15. dir = dir.substring(0, dir.length()-1) + "/";
  16. } else if (dir.charAt(dir.length()-1) != '/') {
  17. dir += "/";
  18. }
  19. dir = java.net.URLDecoder.decode(dir, "UTF-8");
  20. if (new File(dir).exists()) {
  21. String[] files = new File(dir).list(new FilenameFilter() {
  22. public boolean accept(File dir, String name) {
  23. return name.charAt(0) != '.';
  24. }
  25. });
  26. Arrays.sort(files, String.CASE_INSENSITIVE_ORDER);
  27. out.print("<ul class=\"jqueryFileTree\" style=\"display: none;\">");
  28. // All dirs
  29. for (String file : files) {
  30. if (new File(dir, file).isDirectory()) {
  31. out.print("<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + dir + file + "/\">"
  32. + file + "</a></li>");
  33. }
  34. }
  35. // All files
  36. for (String file : files) {
  37. if (!new File(dir, file).isDirectory()) {
  38. int dotIndex = file.lastIndexOf('.');
  39. String ext = dotIndex > 0 ? file.substring(dotIndex + 1) : "";
  40. out.print("<li class=\"file ext_" + ext + "\"><a href=\"#\" rel=\"" + dir + file + "\">"
  41. + file + "</a></li>");
  42. }
  43. }
  44. out.print("</ul>");
  45. }
  46. %>