[java] Read file from webpage
Here is a class I made along time ago for a chat applet. It was intended to work with an ASP or CFML page to get the text off of it. Basically all this thing does is connect to a page, and get the page data. It doesn''t parse the text. That is up to you.
import java.io.*;import java.net.*;public class connector{ private DataInputStream DIS; private URL tmpUrl; private String strProtocol, strHost, strFile; private int intPort; connector(String strURL){ try{tmpUrl = new URL(strURL);} catch(MalformedURLException e){ System.out.println("URL Error: Doesn''t seem to be a valid URL"); } try{ DIS = new DataInputStream(tmpUrl.openConnection().getInputStream()); } catch(IOException e){ System.out.println("I/O Error getting in stream data"); } } connector(String sProt, String sHost, String sFile, int iPort){ try{tmpUrl = new URL(sProt, sHost, iPort, sFile);} catch(MalformedURLException e){ System.out.println("URL Error: Doesn''t seem to be a valid URL"); } try{ DIS = new DataInputStream(tmpUrl.openConnection().getInputStream()); } catch(IOException e){ System.out.println("I/O Error getting in stream data"); } } public String getData() { boolean readMore = true; String tmpStr = null; StringBuffer fullStr = new StringBuffer(); try{ while(readMore){ tmpStr = DIS.readLine(); if(tmpStr == null){readMore = false;} else{fullStr.append(tmpStr);} } } catch(IOException e){ System.out.println("I/O error assigning string data."); } return fullStr.toString(); } public void connect(String strURL){ try{tmpUrl = new URL(strURL);} catch(MalformedURLException e){ System.out.println("URL Error: Doesn''t seem to be a valid URL"); } try{ DIS = new DataInputStream(tmpUrl.openConnection().getInputStream()); } catch(IOException e){ System.out.println("I/O Error getting in stream data"); } } public void connect(String sProt, String sHost, String sFile, int iPort) { try{tmpUrl = new URL(sProt, sHost, iPort, sFile);} catch(MalformedURLException e){ System.out.println("URL Error: Doesn''t seem to be a valid URL"); } try{ DIS = new DataInputStream(tmpUrl.openConnection().getInputStream()); } catch(IOException e){ System.out.println("I/O Error getting in stream data"); } } }
Wrathnut is correct, but you also have to remenber that browsers have security restrictions, and those are a pain in the ***.
----------------------------
^_^
----------------------------
^_^
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement