Automatically updated JSP Download Page
The method is to use one Javabean to search for the folder content every time you access the JSP page. When the folder has been updated, javaBean searches for the updated files and lists the existing files for visitors to download. Description file, "No descrition" is displayed"
The contents of JavaBean are as follows:
/*
* Created on 2004-
* @ Author lingch
*
* Functions:
* 1. Search the directory for files.
* 2. Return the coresponding. txt description content.
*/
Package P;
Import java. Io .*;
Import java. util .*;
Public class files_lookup {
File files;
Vector result;
Public files_lookup ()
{
}
Public vector lookup_files (string path) // --------- path shocould be the downloads dir
{
Vector result = new vector ();
Files = new file (PATH );
String sub_files [];
If (files. isdirectory ())
{
Sub_files = files. List (); // --------- list all files under downloads dir
}
Else
{
Result. Add ("path" + path + "is not a dir ");
Return result;
}
For (INT I = 0; I <sub_files.length; I ++)
{
If (sub_files [I]. lastindexof (". txt")> = 0)
{
Continue; // -------------. TXT are description files. Not for downloading.
}
Result. Add (sub_files [I]);
}
Return result;
}
Public String get_file_description (string file_name) // ---- path shocould be a file name that collecting info
{
File file = new file (file_name );
String description = "no description ";
String f_name = file_name.substring (0, file_name.lastindexof ("."));
F_name = f_name + ". txt"; // ------ change the extension file name to. txt
Filereader RD;
Bufferedreader BRD;
Try {// read. txt content
RD = new filereader (f_name );
BRD = new bufferedreader (RD );
Description = brd. Readline ();
String temp;
While (temp = brd. Readline ())! = NULL)
{
Description + = temp;
}
Brd. Close ();
}
Catch (filenotfoundexception ee)
{
}
Catch (ioexception ee)
{
}
Return description;
}
}
JSP pages can be organized as follows
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. util. *" errorpage = "" %>
<% @ Page Language = "Java" Import = "P. *" %>
<JSP: usebean id = "LB" Scope = "page" class = "P. files_lookup"/>
<HTML>
<Head>
<Title> lingch's download page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<%
String Path = getservletconfig (). getservletcontext (). getrealpath ("//") + "// downloads ";
Vector v = LB. lookup_files (PATH );
If (V! = NULL)
{
For (INT I = 0; I <v. Size (); I ++)
{
%>
<P> file: <a href = "downloads/<% Out. print (v. get (I); %> "> <% Out. print (v. get (I); %> </a> </P>
<P> decription: <% Out. Print (Lb. get_file_description (path + "//" + v. Get (I); %> </P>
& Lt; HR width = "300" & gt;
<%
} //
} // If
%>
<Strong> </strong>
</Body>
</Html>
The file structure of the entire application is as follows:
Downloads/Web-INF/Web. xml
Downloads/Web-INF/classes/P/files_lookup.class
Downloads/downloads. jsp
Downloads/downloads
You only need to put the file to be downloaded into downloads/downloads.
Start Tomcat and press http: // 127.0.0.1/downloads. jsp to access it.