JSP gets the files and directories in the current directory and gets the Windows letter __js

Source: Internet
Author: User
Tags stringbuffer

(i) Get the files and directories in the current directory

Knowledge points


1 Application of the file object
2 Listfiles () method
3 Isdirectory () method, Isfile () method to determine whether a directory or file
4 Conversion String Method tostring
(1)
Create a File object, dir,
Then use the Listfiles () method to return all files in the current directory
String Path=request.getrealpath ("/");
File Dir=new file (path);
File File[]=dir.listfiles ();

(2)
Loop to determine if the return is a directory and display it
Directories are:
<%
for (int i=0;i<file.length;i++)
{
if (File[i].isdirectory ())//
Out.print ("<br>" +file[i].tostring ());
}
%>


(3)
Loop to determine if the return is a directory and display it
File has
<%
for (int i=0;i<file.length;i++)
{
if (File[i].isfile ())
Out.print ("<br>" +file[i].tostring ());
}
%>


Program

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title></title>
<body>
<%
String Path=request.getrealpath ("/");
File Dir=new file (path);
File File[]=dir.listfiles ()////////////////Listfiles () method
%>
Current <%=path%><br>

Directories are:
<%
for (int i=0;i<file.length;i++)
{
if (File[i].isdirectory ())
Out.print ("<br>" +file[i].tostring ());
}
%>
<p> documents are:
<%
for (int i=0;i<file.length;i++)
{
if (File[i].isfile ())
Out.print ("<br>" +file[i].tostring ());
}
%>
</body>

(ii) Get Windows drive letter

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<body>
<%
File[] Roots=file.listroots ();
for (int i=0;i<roots.length;i++)
{
Out.print ("<br>" +roots[i].getpath ());
}
%>
</body>

(iii) integration of two procedures

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*,java.util.*,java.net.*"%>
<%!
Private final static int languageno=0; Language version, 0: Chinese; 1: English
String[] Strdrivers = {"Drive", "Drivers"};
String[] strcurrentfolder= {"Current directory", "Present Folder"};
%>
<body>
<%
String Path=request.getrealpath ("/");
File Dir=new file (path);
File File[]=dir.listfiles ()////////////////Listfiles () method
StringBuffer sb=new StringBuffer (Strdrivers[languageno] + ":");
File roots[]=file.listroots ();
for (int i=0;i<roots.length;i++)
{
Sb.append ("<a href=/" Javascript:doform (', ' +roots[i]+ '//', ', ', ', ' 1 ', ');/">");
Sb.append (roots[i]+ "</a>&nbsp;");
}
Out.println (SB);


%>
<%
Out.print ("<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 #000000 bordercolordark= #FFFFFF >
<tr>
&LT;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 #000000 bordercolordark= #FFFFFF >
<tr>
&LT;TD width= ' 40% ' > directory are:
<%
for (int i=0;i<file.length;i++)
{
if (File[i].isdirectory ())
Out.print ("<br>" +file[i].tostring ());
}
%>
</td>
&LT;TD width= ' 60% ' > Files are:
<%
for (int i=0;i<file.length;i++)
{
if (File[i].isfile ())
Out.print ("<br>" +file[i].tostring ());
}
%>
</td>
</tr>
</table>
</body>

As shown in figure

We are further optimized to achieve

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<% @page import= "java.io.*,java.util.*,java.net.*"%>
<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 a.current{
Background: #FFFFFF;
}

#tabcontentcontainer {
width:100%;
padding:5px;
BORDER:1PX solid black;
}

. tabcontent{
Display:none;
}

</style>
<%!

private final static int languageno=1;//language version, 0: Chinese; 1: English
string[] strcurrentfolder= {"Current directory", "Present Folder"};
String[] strdrivers      = {"Drive", "Drivers"};
String[] strfilename     = {"FileName", "File name"};
String[] strfilesize     = {"File size"};
String[] strlastmodified = {"Final modification", "Last Modified"};
String[] strfileoperation= {"File action", "Operations"};
String[] strfileedit     = {"Modify", "edit"};
String[] strfiledown     = {"Download", "Download"};
String[] strfilecopy     = {"Copy", "Move"};
String[] strfiledel      = {"Delete", "delete"};

String Getdrivers ()
{
StringBuffer sb=new StringBuffer (":");
File roots[]=file.listroots ();
for (int i=0;i<roots.length;i++)
{
Sb.append ("<a href=" "path=" +roots[i]+ "' >");
Sb.append (roots[i]+ "</a> &nbsp;&nbsp;");
}
return sb.tostring ();
}

String Formatpath (String p)
{
StringBuffer sb=new StringBuffer ();
for (int i = 0; i < p.length (); i++)
{
if (P.charat (i) = = '//')
{
Sb.append ("
////");
}
Else
{
Sb.append (P.charat (i));
}
}
return sb.tostring ();
}


public static string Tochinese (String str)
{
try{
if (str==null)
{
Return "";
}else
{
Str=new String (str.getbytes ("Iso8859_1"), "GBK");
return str;
}

}catch (Exception e)
{
Return "";
}
}


%>
<%
Request.setcharacterencoding ("gb2312");
String Strdir =tochinese (request.getparameter ("path"));
if (strdir==null| | Strdir.length () <1)
{
Strdir = Request.getrealpath ("/");
}

StringBuffer sb=new StringBuffer ("");
StringBuffer sbfile=new StringBuffer ("");
Try
{

File objfile = new file (Strdir);
File list[] = Objfile.listfiles ();
if (Objfile.getabsolutepath (). Length () >3)
{
Sb.append ("<tr><td >&nbsp;</td><td><a href= ' path=" +formatPath ( Objfile.getparentfile

(). GetAbsolutePath ()) + "' >");
Sb.append ("To parentfile</a><br>---</td></tr>/r/n");
}
for (int i=0;i<list.length;i++)
{
if (List[i].isdirectory ())
{
Sb.append ("<tr><td>&nbsp;</td><td>");
Sb.append ("<a href= ' path=" +formatpath (List[i].getabsolutepath ()) + "' >" +list[i].getname () + "</a>");
Sb.append ("</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=dt.tolocalestring ();
Sbfile.append ("<tr><td>");
Sbfile.append ("" +list[i].getname ());
Sbfile.append ("</td><td>");
Sbfile.append ("" +strlen);
Sbfile.append ("</td><td>");
Sbfile.append ("" +strdt);
Sbfile.append ("</td><td>");
Sbfile.append ("<a href=>");
Sbfile.append (strfileedit[languageno]+ "</a>");
Sbfile.append ("<a href=>");
Sbfile.append (strfiledown[languageno]+ "</a>");
Sbfile.append ("<a href=>");
Sbfile.append (strfilecopy[languageno]+ "</a>");
Sbfile.append ("<a href=>");
Sbfile.append (strfiledel[languageno]+ "</a>");
Sbfile.append ("</td></tr>/r/n");
}
}
}
catch (Exception e)
{
Out.println ("<font color=red>error:" +e.tostring () + "</font>");
}
%>
<table border= ' 1 ' width= ' 100% ' bgcolor= ' #FFFFFF ' cellspacing=0 cellpadding=5 #000000 bordercolordark= #FFFFFF >
<tr>
&LT;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 #000000 bordercolordark= #FFFFFF >
<tr>
&LT;TD width= ' 20% ' valign= ' top ' >
<table >
<tr><td><%=sb%></td>
</tr>
</table>
</td>
&LT;TD width= ' 70% ' align= ' center ' valign= ' top ' >
<table width= "100%" border= "1" cellspacing= "1" cellpadding= "4" bordercolorlight= "#000000" bordercolordark= "# FFFFFF ">
<tr>
&LT;TD width= "26%" align= "center" ><%=strfilename[languageno]%></td>
&LT;TD width= "19%" align= "center" ><%=strfilesize[languageno]%></td>
&LT;TD width= "29%" align= "center" ><%=strlastmodified[languageno]%></td>
&LT;TD width= "26%" align= "center" ><%=strfileoperation[languageno]%></td></tr>
&LT;TR&GT;&LT;TD width= "25%" align= "center" ><%=sbfile%></td></tr>
</table>
</td>
</tr>
</table>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.