Click to See Complete Forum and Search --> : Downloading
afmook
05-09-2006, 11:03 AM
Ok, I've searched on the forums and also googled a bunch and can't find any easy examples. Most have either depreciated functions/etc or are giving me errors.
All I want is a very simple function that will download whatever url (passed in by a string) and simply put all of the data for that page/file/whatever into a single string.
Basically:
download file
be able to parse through the entire file as one big entity
I'd really appreciate any help you could give.
Khalid Ali
05-09-2006, 07:14 PM
what have u done to solve this issue, e.g like code examples etc
afmook
05-09-2006, 08:09 PM
Honestly you don't want to see it. I was in the middle of copying/modifying a new example in the middle of the old one, so the code isn't usable and doesn't make much sense.
But since you asked...
public String Download(String DownloadURL)
{
String Contents = null;
try {
URL url = new URL(DownloadURL);
URLConnection connection = url.openConnection();
DataInputStream in = new DataInputStream(u.getInputStream());
for (;;)
{
String line = in.readLine();
if (line == null) break;
System.out.println(" " + line);
}
}catch (MalformedURLException e){
//TODO
}catch (IOException e){
//TODO:FileNotFoundException caught here
}
return Contents;
}
Khalid Ali
05-09-2006, 09:29 PM
readline has been deprecated...loooong ago in DataInputStream class, try the code below. It will work. make sure that u provide a valid web url
public String readDataFromURL(String dataURL) {
String dataStr=null;
try {
URL url = new URL(dataURL);
URLConnection connection = url.openConnection();
//comment out 2 lines below if you are want ur java app to work as browser
//connection.setDoOutput(true);
//connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312");
BufferedReader buffReader = new BufferedReader(new InputStreamReader(url.openStream()));
String line="";
while((line=buffReader.readLine())!=null){
dataStr+=line+"\n";
}
System.out.println(dataStr);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return dataStr;
}
navmailin
05-10-2006, 02:00 AM
hey cool code
afmook
05-10-2006, 01:26 PM
Appreciate the code. I'll try it as soon as I get home and get back to you.
afmook
05-10-2006, 10:17 PM
Works great, thanks a ton!