@@ -151,6 +151,7 @@ public class TinyWebServer extends Thread {
151151 public static String SERVER_IP ="localhost" ;
152152 public static int SERVER_PORT =9000 ;
153153 public static boolean isStart =true ;
154+ public static String INDEX_FILE_NAME ="index.html" ;
154155
155156
156157 public TinyWebServer (final String ip , final int port ) throws IOException {
@@ -281,7 +282,7 @@ public void processLocation(DataOutputStream out, String location, String postDa
281282 case "/" :
282283 //root location, server index file
283284 CONTENT_TYPE = "text/html" ;
284- data =readFile (WEB_DIR_PATH +"/index.php" );
285+ data =readFile (WEB_DIR_PATH +"/" + INDEX_FILE_NAME );
285286 constructHeader (out , data .length () + "" , data );
286287 break ;
287288 default :
@@ -528,6 +529,7 @@ public String readFile(String fileName){
528529 }
529530 }catch (Exception er ){
530531 pageNotFound ();
532+ return "" ;
531533 }
532534 return content ;
533535 }
@@ -538,6 +540,7 @@ public static void init(String ip,int port,String public_dir){
538540 SERVER_IP =ip ;
539541 SERVER_PORT =port ;
540542 WEB_DIR_PATH =public_dir ;
543+ scanFileDirectory ();
541544
542545 }
543546
@@ -568,7 +571,31 @@ public static void stopServer(){
568571 }
569572 }
570573
571- /*//use for testing
574+
575+ //scan for index file
576+ public static void scanFileDirectory (){
577+ boolean isIndexFound =false ;
578+ try {
579+ File file =new File (WEB_DIR_PATH );
580+ if (file .isDirectory ()){
581+ File [] allFiles =file .listFiles ();
582+ for (File allFile : allFiles ) {
583+ //System.out.println(allFile.getName().split("\\.")[0]);
584+ if (allFile .getName ().split ("\\ ." )[0 ].equalsIgnoreCase ("index" )){
585+ TinyWebServer .INDEX_FILE_NAME =allFile .getName ();
586+ isIndexFound =true ;
587+ }
588+ }
589+ }
590+
591+ }catch (Exception er ){}
592+
593+ if (!isIndexFound ){
594+ System .out .println ("Index file not found !" );
595+ }
596+ }
597+
598+ /* //use for testing
572599 public static void main(String[] args) {
573600 try {
574601
0 commit comments