- import java.net.URL;
- import java.net.URLConnection;
- import java.io.*;
- import java.util.Date;
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- ReadHtml rh = new ReadHtml();
- rh.getWebPage();
- rh.displayAll();
- }
- }
- class ReadHtml {
- private URL u;
- private URLConnection uc;
- String addr = new String(); //用来存放网址
- Scanner sc = new Scanner(System.in); //用来输入网址
- File f = new File("webpage.html"); //创建一个存放网页的文件
- InputStreamReader in; //创建一个文件输入流
- OutputStreamWriter out; //创建一个文件输出流
- char[] buf = new char[1024]; //每次读写1k内容
- public ReadHtml() {
- try {
- System.out.println("请输入文件的URL地址:");
- addr = sc.nextLine();
- u = new URL(addr); //定义URL对象
- uc = u.openConnection(); //获得一个URLConnection对象
- uc.connect(); //连接
- in = new InputStreamReader(uc.getInputStream());
- out = new OutputStreamWriter(new FileOutputStream(f));
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- public void getWebPage() {
- try {
- while(in.read(buf, 0, buf.length) > 0) {
- out.write(buf, 0, buf.length);
- }
- in.close();
- out.close(); //关闭流
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- //打印所有信息
- public void displayAll() {
- System.out.println("内容类型: "+uc.getContentType());
- System.out.println("内容编码: "+uc.getContentEncoding());
- System.out.println("内容长度: "+uc.getContentLength());
- System.out.println("创建日期: "+new Date(uc.getDate()));
- System.out.println("最后修改日期: "+new Date(uc.getLastModified()));
- System.out.println("终止日期: "+new Date(uc.getExpiration()));
- }
- }
studying