| 1 16 package dlog4j.tags; 17 18 import java.io.File ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.tagext.TagSupport ; 24 25 import org.apache.commons.lang.StringUtils; 26 27 31 public class ListFilesTag extends TagSupport { 32 33 public final static String WEB_PATH = "{webapp}"; 34 String path; 35 String extend; 36 boolean exclude = false; 37 String pathId; 38 39 public int doStartTag() throws JspException { 40 String dir = StringUtils.replace(path, 41 WEB_PATH, 42 pageContext.getServletContext().getRealPath("")); 43 pageContext.setAttribute(pathId, dir); 44 File f = new File (dir); 45 if(f.exists() && f.isDirectory()){ 46 String [] fns = f.list(); 47 List lfns = new ArrayList (); 48 for(int i=0;i<fns.length;i++){ 49 if(extend!=null){ 50 if(exclude && fns[i].endsWith(extend)) 51 continue; 52 else 53 if(!exclude && !fns[i].endsWith(extend)) 54 continue; 55 } 56 lfns.add(fns[i]); 57 } 58 pageContext.setAttribute(id, lfns); 59 } 60 else 61 pageContext.getServletContext().log("目录["+path+"]不存在"); 62 return SKIP_BODY; 63 } 64 65 public void release() { 66 path = null; 67 extend = null; 68 exclude = false; 69 } 70 public boolean isExclude() { 71 return exclude; 72 } 73 public void setExclude(boolean exclude) { 74 this.exclude = exclude; 75 } 76 public String getExtend() { 77 return extend; 78 } 79 public void setExtend(String extend) { 80 this.extend = extend; 81 } 82 public String getPath() { 83 return path; 84 } 85 public void setPath(String path) { 86 this.path = path; 87 } 88 public String getPathId() { 89 return pathId; 90 } 91 public void setPathId(String pathId) { 92 this.pathId = pathId; 93 } 94 } 95 | Popular Tags |