So i was browsing some of my old applications/demo classes and found this that i could share with you all. It reads a file from an URL and saves it in a file into your disk. Default file to read:
http://engineeringserver.com/randomfile.txtOutput should be something like this:
HTTP/1.1 200 OK
Date: Wed, 10 Jun 2009 09:17:12 GMT
Server: Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.8
Last-Modified: Wed, 10 Jun 2009 09:17:06 GMT
ETag: "22eaccc-5a-46bfaf0652880"
Accept-Ranges: bytes
Content-Length: 90
Connection: close
Content-Type: text/plain
You just downloaded a textfile from
http://www.engineeringserver.com /
www.javaforums.net GeSHi (java):
/*--------------------------------------------------------------------------
FileUpdater.java
******************
By: HappyFace http://www.engineeringserver.com
Contact: info [@] engineeringserver.com
Version: 8/August/2007
Last updated: 10/June/2009
"*****************
Description:
A demo how to read data from an url.
Reference: http://en.wikipedia.org/wiki/List_of_HTTP_headers
To do:
-
//----------------------------------------------------------------------*/
package console;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class HttpRequester {
HttpRequester gr = new HttpRequester();
gr.Update();
}
public void Update(){
String host =
"www.engineeringserver.com";
String saveLocation =
"c:\\webPage.txt";
try {
p.print("GET /randomfile.txt HTTP/1.0\r\n");
p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n");
p.print("Host: " + host + "\r\n");
p.print("Connection: close\r\n\r\n");
Scanner sc = new Scanner(in);
int lines = 0;
while (sc.hasNextLine()){
String readData = sc.
nextLine();
System.
out.
println(lines +
") " + readData
);
bw.write(readData);
bw.newLine();
lines++;
}
bw.close();
fwe.close();
Desktop.
getDesktop().
open(new File(saveLocation
));
e.printStackTrace();
e.printStackTrace();
}
}
}
Created by GeSHI 1.0.7.20