Another way to use the URL class is to download a document from an HTTP URL. When you start the client connects to a Web server. In the client code, you:
Create a URL object.
Create an InputStream object from the URL object.
Use read on the InputStream object to read in the document.
The following code sample works by:
Reading the entire document into Adaptive Server memory.
Creating a new InputStream on the document in Adaptive Server memory.
import java.io.*;
import java.net.*;
public class URLprocess {
public static InputStream readURL()
throws Exception {
URL u = newURL(“http://www.xxxx.com”);
InputStream in = u.openStream();
//This is the same as creating URLConnection, then //calling getInputStream(). In ASE you need to read
//the entire document into memory, then create an //InputStream on the in-memory copy.
int n=0,off;
byte b[]=new byte[50000];
for(off=0;(off<b.length512)
&&((n=in.read(b,off,512)!=-1);off+=n){}
System.out.println(“Number of bytes read :” + off);
in.close();
ByteArrayInputStream test =
new ByteArrayInputStream(b,0,off);
return (InputStream) test;
}
}
After you create the new InputStream class, you can install this class and use it to read a text file into the database, inserting data into a table, as in the following example.
create table t (cl text) go insert into t values (URLprocess.readURL()) go Number of bytes read :40867
select datalength(cl) from t go
------------
40867