www.pudn.com > java实验的web浏览器源代码支持mime.rar > dealLocalFile.java


package container.staticsou;  
import java.io.*;  
import java.text.DateFormat;  
import java.text.SimpleDateFormat;  
import java.text.*;  
import java.util.Locale;  
import java.util.Date;  
import http.*;  
 
public class dealLocalFile{  
 
File localfile=null;  
String RequestSouc;  
byte[] GetContent;  
boolean ListDirectory=true;  
 
public dealLocalFile(String RequestSouc){  
boolean defaultname=false;  
this.RequestSouc=RequestSouc;  
if ( RequestSouc.endsWith("/") ){  
String fn[]=jwsconf.HttpConfM("DirectoryIndex");  
for (int i=0;ilocalfile = new File(jwsconf.HttpConf("DocumentRoot"),RequestSouc+fn[i]);  
if ( FileExists()==2 ){  
defaultname=true;  
break;}  
}//end for  
 
if (defaultname==false){  
localfile = new File( jwsconf.HttpConf("DocumentRoot"),RequestSouc.substring(0,RequestSouc.length()-1) );}  
}else{  
localfile=new File(jwsconf.HttpConf("DocumentRoot"),RequestSouc);  
if ( FileExists()==1){  
ListDirectory=false;}  
}  
}  
 
public byte[] GetContent(){  
 
if( localfile.isDirectory() ){  
DirectoryList();  
}else{  
ReadLocalFile();}  
return GetContent;  
 
}//end GetContent()  
 
 
private void ReadLocalFile(){  
GetContent = new byte[(int) localfile.length()];  
 
try{  
FileInputStream readfile = new FileInputStream(localfile);  
try{  
readfile.read( GetContent,0,readfile.available() );  
readfile.close();  
}catch (IOException e){}  
 
}catch (FileNotFoundException e){}  
 
}//end ReadLocalFile()  
 
 
private void DirectoryList(){  
 
String filenames[]=localfile.list();  
String DirectoryHtml;  
File checkf;  
String DataString="";  
 
String ParentDirectory=RequestSouc.substring(0, RequestSouc.lastIndexOf('/',RequestSouc.length()-2) );  
DirectoryHtml="";  
DirectoryHtml+=" 
Index of "+RequestSouc+" 
";  
DirectoryHtml+="Name\t\t\tLast modified\t\tSize Description\r\n";  
 
DirectoryHtml+="--------------------------------------------------------------------------------Parent Directory\r\n";  
 
for(int i=0;icheckf = new File(jwsconf.HttpConf("DocumentRoot"),filenames[i]);  
if ( checkf.isDirectory() ){  
filenames[i]+='/';} //如果是目录就在后面加 '/'  
DataString = new SimpleDateFormat("dd MMM yyyy HH:mm:ss 'GMT'",Locale.US).format(new Date(checkf.lastModified()));  
 
DirectoryHtml+="" + filenames[i] + "\t\t\t"  
+ DataString  
+ "\t\t"  
+ checkf.length() + "\r\n";  
}  
DirectoryHtml+=" 
 
-------------------------------------------------------------------------------- 
";  
GetContent = DirectoryHtml.getBytes();  
 
}  
 
 
public int FileExists(){  
 
if (localfile.exists() && localfile.isDirectory() && ListDirectory==true) {  
return 1;}  
 
if (localfile.exists() && !localfile.isDirectory()){  
return 2;  
}else{return 0;}  
}//end FileExists()  
 
 
}