This article describes the method of JSP programming to obtain files, directories and Windows drive letters in the current directory. Share it for your reference, as follows:
(I) Get the files and directories in the current directory
Knowledge points
1 Application of file object
2 listFiles() method
3 isDirectory() method, isFile() method determines whether it is a directory or a file
4 Convert string method toString
(1) Create a file object dir,
Then use the listFiles() method to return all files in the current directory
String path=("/"); File dir=new File(path); File file[]=();
(2) Loop to determine whether the returned directory is a directory and display it:
<% for(int i=0;i<;i++) { if(file[i].isDirectory())// ("<br>"+file[i].toString()); } %>
(3) Loop to determine whether the returned directory is a directory and display it:
<% for(int i=0;i<;i++) { if(file[i].isFile()) ("<br>"+file[i].toString()); } %>
program:
<%@ page contentType="text/html;charset=GB2312"%> <%@ page import=".*"%> <html> <title></title> <body> <% String path=("/"); File dir=new File(path); File file[]=();//Return the file in the specified directory using the listFiles() method of the file object%> current<%=path%><br> The directory has: <% for(int i=0;i<;i++) { if(file[i].isDirectory()) ("<br>"+file[i].toString()); } %> <p>There are files: <% for(int i=0;i<;i++) { if(file[i].isFile()) ("<br>"+file[i].toString()); } %> </body> </html>
(II) Get Windows Drive Letter
<%@ page contentType="text/html;charset=GB2312"%> <%@ page import=".*"%> <html> <body> <% File[] roots=(); for(int i=0;i<;i++) { ("<br>"+roots[i].getPath()); } %> </body> </html>
(III) Merge the two programs
<%@ page contentType="text/html;charset=GB2312"%> <%@ page import=".*,.*,.*" %> <%! private final static int languageNo=0; //Language version, 0: Chinese; 1: EnglishString[] strDrivers = {"Driver","Drivers"}; String[] strCurrentFolder= {"Current Directory","Current Folder"}; %> <html> <body> <% String path=("/"); File dir=new File(path); File file[]=();//Return the file in the specified directory using the listFiles() method of the file objectStringBuffer sb=new StringBuffer(strDrivers[languageNo] + " : "); File roots[]=(); for(int i=0;i<;i++) { (" <a href=/"javascript:doForm('','"+roots[i]+"//','','','1','');/">"); (roots[i]+"</a>&nbsp;"); } //(sb); %> <% //("<table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF><tr><td>"+strCurrentFolder[languageNo]+":<br><b>"+path+"</td><td>"+sb+"</td></tr></table>"); %> <table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF> <tr> <td width='40%'><%=strCurrentFolder[languageNo]%>:<br><%=path %></td><td width='60%'><%=strDrivers[languageNo] %>:<%=sb%></td></tr> </table> <table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF> <tr> <td width='40%'>The directory has: <% for(int i=0;i<;i++) { if(file[i].isDirectory()) ("<br>"+file[i].toString()); } %> </td> <td width='60%'>There are files: <% for(int i=0;i<;i++) { if(file[i].isFile()) ("<br>"+file[i].toString()); } %> </td> </tr> </table> </body> </html>
We further optimize and achieve
<%@ page contentType="text/html; charset=GBK" %> <%@page import=".*,.*,.*" %> <head> <style type="text/css"> td,select,input,body{font-size:9pt;} A { text-decoration: none } #tablist{ padding: 5px 0; margin-left: 0; margin-bottom: 0; margin-top: 0.1em; font:9pt; } #tablist li{ list-style: none; display: inline; margin: 0; } #tablist li a{ padding: 3px 0.5em; margin-left: 3px; border: 1px solid ; background: #FFFFFF; } #tablist li a:link, #tablist li a:visited{ color: navy; } #tablist li { background: #FFFFFF; } #tabcontentcontainer{ width: 100%; padding: 5px; border: 1px solid black; } .tabcontent{ display:none; } </style> </head> <%! private final static int languageNo=1; //Language version, 0: Chinese; 1: EnglishString[] strCurrentFolder= {"Current Directory","Current Folder"}; String[] strDrivers = {"Driver","Drivers"}; String[] strFileName = {"File Name","File Name"}; String[] strFileSize = {"File Size","File Size"}; String[] strLastModified = {"Last Modification","Last Modified"}; String[] strFileOperation= {"File Operation","Operations"}; String[] strFileEdit = {"Revise","Edit"}; String[] strFileDown = {"download","Download"}; String[] strFileCopy = {"copy","Move"}; String[] strFileDel = {"delete","Delete"}; String getDrivers() { StringBuffer sb=new StringBuffer(": "); File roots[]=(); for(int i=0;i<;i++) { ("<a href='?path="+roots[i]+"'>"); (roots[i]+"</a> &nbsp;&nbsp;"); } return (); } String formatPath(String p) { StringBuffer sb=new StringBuffer(); for (int i = 0; i < (); i++) { if((i)=='//') { ("////"); } else { ((i)); } } return (); } public static String toChinese(String str) { try{ if(str==null) { return ""; }else { str=new String(("ISO8859_1"),"GBK"); return str; } }catch(Exception e) { return ""; } } %> <% ("gb2312"); String strDir =toChinese( ("path")); if(strDir==null||()<1) { strDir = ("/"); } StringBuffer sb=new StringBuffer(""); StringBuffer sbFile=new StringBuffer(""); try { File objFile = new File(strDir); File list[] = (); if(().length()>3) { ("<tr><td >&nbsp;</td><td><a href='?path="+formatPath( ().getAbsolutePath())+"'>"); ("To ParentFile</a><br>- - - - </td></tr>/r/n"); } for(int i=0;i<;i++) { if(list[i].isDirectory()) { ("<tr><td>&nbsp;</td><td>"); ("<a href='?path="+formatPath(list[i].getAbsolutePath())+"'>"+list[i].getName()+"</a>"); ("</td></tr>"); } else { String strLen=""; String strDT=""; long lFile=0; lFile=list[i].length(); if(lFile>1000000) { lFile=lFile/1000000; strLen="" + lFile + " M"; } else if(lFile>1000) { lFile=lFile/1000; strLen="" + lFile + " K"; } else { strLen="" + lFile + " Byte"; } Date dt=new Date(list[i].lastModified()); strDT=(); ("<tr><td>"); (""+list[i].getName()); ("</td><td>"); (""+strLen); ("</td><td>"); (""+strDT); ("</td><td>"); (" <a href=>"); (strFileEdit[languageNo]+"</a> "); (" <a href=>"); (strFileDown[languageNo]+"</a> "); (" <a href=>"); (strFileCopy[languageNo]+"</a> "); (" <a href=>"); (strFileDel[languageNo]+"</a> "); ("</td></tr>/r/n"); } } } catch(Exception e) { ("<font color=red>error: "+()+"</font>"); } %> <table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF> <tr> <td width='20%'><%=strCurrentFolder[languageNo]%>:<br><%=strDir %></td><td width='70%'><%=strDrivers[languageNo] %><%=getDrivers()%> </td> </tr> </table> <table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF> <tr> <td width='20%'valign="top"> <table > <tr><td><%=sb%></td> </tr> </table> </td> <td width='70%'align="center" valign="top"> <table width="100%"border="1" cellspacing="1" cellpadding="4" bordercolorlight="#000000" bordercolordark="#FFFFFF"> <tr> <td width="26%" align="center"><%=strFileName[languageNo] %></td> <td width="19%" align="center"><%=strFileSize[languageNo] %></td> <td width="29%" align="center"><%=strLastModified[languageNo] %></td> <td width="26%" align="center"><%=strFileOperation[languageNo] %></td></tr> <tr><td width="25%" align="center"><%=sbFile %></td></tr> </table> </td> </tr> </table>
I hope this article will be helpful to everyone's jsp programming.