Read files from a directory on the server the file name files modified date and file size displayed in the foreground and can be viewed and downloaded
First step: Read the files in the file directory and arrange the files by the time from the big to the small
Public arraylist<file> Getlogs () {
TODO auto-generated Method Stub
arraylist<file> tomcatlogs = new arraylist<file> ();
File path = new file ("");
File NewPath = new file (Path.getabsolutepath () + "\\log"); The default is Tomcat's default working directory log
file[] files = newpath.listfiles ();
File Tempfile = new file ("");
for (int i=0;i<files.length;i++) {//Use bubble Sort method to sort files by modified time
for (int j=i;j<files.length;j++) {
if (Files[i].lastmodified () <files[j].lastmodified ()) {
Tempfile = Files[i];
Files[i] = Files[j];
FILES[J] = tempfile;
}
}
}
for (File file:files)
{
Tomcatlogs.add (file);
}
return tomcatlogs;
}
Step two: The list of acquired files is displayed in the foreground using the technology: El expression as shown, and provides the interface search function.
Code Show:
<link rel= "stylesheet" type= "Text/css" href= "${ctx}/css/bca/dialog.css"/>
<style>
. content{
margin-top:20px;
}
#commands {
margin-top:10px;
padding-left:0px;
}
#commands li{
List-style:none;
Backgroud: #000;
height:40px;
line-height:40px;
Color: #000;
padding-left:10px;
width:100%;
}
. selectedli{
Background: #40C5F0;
border-left:5px solid #0294AC;
}
. Selectedli a{
Color: #fff;
Text-decoration:none;
}
</style>
<body>
<script type= "Text/javascript" >
function Gettomcatlog (logname,type,size) {
if (type = = "Down") {
var urllogshow = sprintf ("${ctx}/log/downloadlog.do?logname=%s&type=%s", Logname,type);
window.open (urllogshow);
}else{
var intSize = parseint (size);
if (size>1024*2) {
ShowMessage (' file is too large to open in the browser, please download and view! ‘);
Return
}
var urllogshow = sprintf ("${ctx}/pages/log/tomcatlogshow.page?logname=%s&type=%s", Logname,type);
window.open (urllogshow);
}
}
var findsysname = function () {
$ ("#commands li"). Hide ();
var keyword = $ ("#log_key"). Val ();
if (Keyword.length = = 0) {
$ ("#commands Li"). Show ();
} else {
$ ("#commands li:contains (" + keyword + ")"). Show ();
}
}
</script>
<div class= "col-sm-3" style= "Padding-right:0px;width:75%;margin-left:16%;float:inherit;" >
<fieldset class= "Fieldsetstyle" id= "Showcommands" >
<div class= "Ct_control_bar" >
<input class= "Box-height" style= "width:30%" placeholder= "search" type= "text" name= "Log_key" id= "Log_key" >
<input class= "btn btn-primary btn-xs" type= "button" value= "${bcafn:_ (" Search ")}" id= "Logselect" onclick= " Findsysname () ">
</div>
<div id= "Grouptree" name= "Grouptree" class= "Systemtree" >
<ul id= "Commands" class= "Sys_ula" >
<li >
<div style= "Width:250px;float:left;" > File name </div>
<div style= "width:200px;float:left;" align= "Left" > Modified date </div>
<div style= "width:100px;float:left;" align= "right" > Size </div>
</li>
</br>
<c:foreach var= "Log" items= "${logs}" >
<li >
<div style= "Width:250px;float:left;" >${log.getname ()}</div>
<div style= "width:200px;float:left;" align= "left" id= "${log.getname ()}" >${log.lastmodified ()}</div>
<div style= "width:100px;float:left;" align= "right" > <fmt:formatnumber type= "number" value= "${log.length ()/ 1024x768} "maxfractiondigits=" 0 "/>kb</div>
<input class= "btn btn-primary btn-xs" type= "button" value= "${bcafn:_ (" Download ")}" onclick= "Gettomcatlog (' ${ Log.getname ()} ', ' Down ', ' ${log.length ()/1024} ') ' >
<c:if test= "${log.length ()/1024<= 1024*2}" >
<input class= "btn btn-primary btn-xs" type= "button" value= "${bcafn:_ (" View ")}" onclick= "Gettomcatlog (' ${ Log.getname ()} ', ' Show ', ' ${log.length ()/1024} ') ' >
</c:if>
</li>
</c:forEach>
<script type= "Text/javascript" >
$ (document). Ready (function () {
<c:foreach var= "Log" items= "${logs}" >
var Spanid = "${log.getname ()}";
var date = Getformatdatebylong (parseint ("${log.lastmodified ()}"));
document.getElementById (spanid). InnerHTML = date;
</c:forEach>
});
</script>
</ul>
</div>
</fieldset>
</div>
<div class= "Content content-btn" style= "margin-top:0px; margin-right:80px;margin-bottom:10px; " >
<input id= "Cancel" type= "button" value= "${bcafn:_ (" Close ")}" class= "Btn Btn-default"/>
</div>
</body>
Technical Features: (1), use El expression in JavaScript, convert millisecond time to date format eg:1462520365327 to 2016-05-06 15:39:25.
(2), jquery, JavaScript page search function.
Step three, view or download
public string Showordownlog (string logname,string type,httpservletresponse response) {
TODO auto-generated Method Stub
String loghsow= "";
if (Stringutils.isnotempty (LogName)) {
File path = new file ("");
try {
InputStream fis = new FileInputStream (Path.getabsolutepath () + "\\log\\" +logname);
if ("Show". Equals (Type)) {//view
BufferedReader reader= New BufferedReader (FIS, "GBK"); According to the file format to decide
String Line=null;
For (;(line=reader.readline ())!=null;) {loghsow+=line+ "\ n";}
Reader.close ();
}else{//Download
Response.setcontenttype ("Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Response.setheader ("Content-disposition", "attachment; Filename= "+ urlencoder.encode (logName," utf-8 "));
Response.setheader ("Cache-control", "max-age=0");
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Response.getoutputstream (). write (buffer);
Response.getoutputstream (). Flush ();
Response.getoutputstream (). Close ();
Fis.close ();
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
return Loghsow;
}
Java reads all the files in a directory and displays the relevant file information through El Expression, JS provides page search and view Download function