java根据url获取流

上一篇 / 下一篇  2014-05-06 13:59:31 / 个人分类:Java学习

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public class URLApp
{
   public void display()
   {
    byte buf[] = new byte[1000];
    try
   {
    System.out.print("Please Input URL address:");
    //Input user's URL by manually in Console
    InputStream consoleis = System.in;
    int count = consoleis.read(buf);
    String addr = new String(buf,0,count);
    
    //create URL object
    URL url = new URL (addr);
    //Create URLConnection object, return URLConnection object by openConnection() method
    URLConnection conn = url.openConnection();
    //setup connection
    conn.connect();
    //Display the related information
    System.out.println("Content Type: "+ conn.getContentType());
    System.out.println("Content Length: "+ conn.getContentLength());
    System.out.println("Creation Date: "+ new Date(conn.getDate()));
    System.out.println("Last Modified Date: "+ new Date(conn.getLastModified()));
    System.out.println("Expiration Date: "+ new Date(conn.getExpiration()));

    InputStream is = conn.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String str = null;
    while((str = br.readLine()) != null)
    {
      System.out.println(str);
    }
     
     
   }
   catch(IOException e)
   {
    System.out.println(e);
     }
}
  
   public static void main(String[] args)
   {
       URLApp app = new URLApp();
       app.display();
   }
}
 

TAG:

 

评分:0

我来说两句

Open Toolbar