Class insert comprises of two methods. This class inserts advertisment(.jpg) files in the html pages.
Method advtinsert checks \ advt directory for any .gif / .jpg files. Html tags referencing these files are to be inserted in the .htm files. It therefore employs two loops, the outer for the number of files listed in the globalfiledbase[ ] and the inner for the number of advertisements (.gif /.jpg) in the advt directory on the server side.
Call to method insertadvt is made to do the actual insertion of the proper tags. Multiple calls to insertadvt will insert that many tags in a file.
Method insertadvt takes two strings, a filename and an image filename as the parameters. If the file has an html extension it is opened and scanned for "<HTML>" tag. A valid html file starts with string "<HTML>". Any browser worth its name recognises this tag. It also recognises the <IMG SRC="filename"></IMG SRC> tag. This tag is to be inserted in the html file after the "<HTML>" tag. It then forms inserts the second parameter at appropriate place in the "<IMG SRC=" tag. This tag, now referencing the image filename, is appended after the "<HTML>" tag.
// filename: insert.java
import java.io.*; import java.util.*; import java.lang.*; public class insert { public insert( ) //constructor for future consideration { } public void advtinsert( ) throws Exception { String[ ] adarray = advtpush.select( ); int ladarray = adarray.length; sint.print("\n Inserting advts : \n\t"); for(int htmfilecount=0; htmfilecount < InetClient.fileindex; htmfilecount++) { for(int adfilecount = 0; adfilecount < ladarray; adfilecount++) { if(InetClient.globalfiledbase[htmfilecount].lastIndexOf(".htm")!=-1) { if (InetClient.f_modified[htmfilecount] == true) insertadvt(InetClient.globalfiledbase[htmfilecount],adarray[adfilecount]); } } } } static void insertadvt(String filename, String adfilename) throws Exception { String sr = "<HTML>"; String arraytostring; char aa[ ]; int k; int ptr; FileInputStream fip = new FileInputStream(filename); FileOutputStream fop = new FileOutputStream("dummy.htm"); File fp = new File(filename); int g; aa = new char[6]; if(fp.exists( ) && fp.isFile( )) // && filename.lastIndexOf(".htm")!=-1) { while((g = fip.read( ))!=-1) { fop.write(g); for(k=1;k < =5;k++) { aa[k-1] = Character.toUpperCase(aa[k]); } aa[5] = Character.toUpperCase((char)g); //aa[7] arraytostring = new String(aa); ptr = sr.compareTo(arraytostring); arraytostring=arraytostring.trim( ); if(ptr == 0) { String append = "<IMG SRC="+adfilename+"></IMG SRC><hr>"; for(int kk=0;kk < append.length( );kk++) fop.write(append.charAt(kk)); } //end ptr 0 } //while ends }//end of if file fop.close( );//very important to close otherwise collects garbage from previous files fip.close( ); FileInputStream fis = new FileInputStream("dummy.htm"); int qq; FileOutputStream fos = new FileOutputStream(filename); while((qq=fis.read( ))!=-1) { fos.write(qq); } fis.close( ); fos.close( ); File lost = new File("dummy.htm"); lost.delete( ); } }