CConnection

Class CConnection makes a connection to an URL and downloads a specified page. Method getwebPage creates a URLConnection object that contains a connection to the remote object referred to by the URL. It also invokes the appropriate protocol handler. It then gets an InputStream that reads from the URLConnection object. It creates a new DataInputStream to allow us to read primitive Java data types from the input stream in a portable way. A file with the filename (isolated from the URL) is opened. Unsigned bytes are read from the datainputstream and written to the file. An EOFException tells us that the end of the stream is reached. The file name is added to the gobalfiledbase. Absolute path of the saved file is returned.
// filename CConection.java import java.net.*; import java.io.*; import java.util.*; import java.awt.*; public class CConnection { public String getwebPage(URL u1,String page) throws Exception { URLConnection c; InputStream n; DataInputStream d; String str; File fpweb; FileOutputStream fop; String filesaved_hd; boolean ffff; Properties p = new Properties( ); URL u2; boolean donot_connect = false ; String pagetrimmed; pagetrimmed = settrims.mytrims(page); try{ c = u1.openConnection( ); if (InetClient.connected_once == false) { InetClient.globalfiledbase_path[InetClient.fileindex] = u1.toString( ); InetClient.f_date[InetClient.fileindex] = c.getLastModified( ); System.out.println("Path: "+InetClient.globalfiledbase_path[InetClient.fileindex]+" InetClient.f_date: "+InetClient.f_date[InetClient.fileindex]); } else { System.out.println("Path: "+InetClient.globalfiledbase_path[InetClient.fileindex]+" InetClient.f_date: "+InetClient.f_date[InetClient.fileindex]); System.out.println("Modified InetClient.f_date: "+c.getLastModified( )); if (c.getLastModified( ) == InetClient.f_date[InetClient.fileindex]) return ""; //send NULL string if no modification in the file InetClient.f_date[InetClient.fileindex] = c.getLastModified( ); } n = c.getInputStream( ); d = new DataInputStream(n); fop = new FileOutputStream(System.getProperty("user.dir")+"\\"+pagetrimmed); //*/"d:\\abhi\\src\\java\\io\\"+pagetrimmed); int readbyte=0; InetClient.f_modified[InetClient.fileindex] = true; while((readbyte = d.readUnsignedByte( )) != -1) { fop.write(readbyte); } }//end of try catch (Exception x){System.out.println("\n -------- File read error. --------\n"+x); } fpweb = new File(System.getProperty("user.dir")+"\\"+pagetrimmed); //*/"d:\\abhi\\src\\java\\io\\",pagetrimmed); filesaved_hd = fpweb.getAbsolutePath( ); // InetClient.len_array[InetClient.arrayindex] = fpweb.length( ); // InetClient.arrayindex++; return filesaved_hd; } }

next >>

Index