Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - URLConnection 的 getInputStream() 方法



描述

Java URLConnection 的 getInputStream() 方法返回一個輸入流,用於讀取此開啟的連線。如果在資料可用之前讀取超時過期,則從返回的輸入流讀取時可能會丟擲 SocketTimeoutException 異常。

宣告

以下是 java.net.URLConnection.getInputStream() 方法的宣告

public InputStream getInputStream() throws IOException

引數

返回值

一個用於讀取此開啟連線的輸入流。

異常

IOException − 如果獲取內容時發生 I/O 錯誤。

UnknownServiceException − 如果協議不支援輸入。

示例 1

以下示例演示瞭如何使用 Java URLConnection 的 getInputStream() 方法處理使用 https 協議的有效 url。在這個例子中,我們建立了一個 URL 類的例項。使用 url.openConnection() 方法,我們得到了 URLConnection 例項。使用 getInputStream(),我們獲取了網站主頁的內容並打印出來。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上面的程式,這將產生以下結果:

輸出

<!DOCTYPE html><html lang="en"><head>	<title>Online Tutorials, Courses, and eBooks ....</body></html>

示例 2

以下示例演示瞭如何使用 Java URLConnection 的 getInputStream() 方法處理使用 http 協議的有效 url。在這個例子中,我們建立了一個 URL 類的例項。使用 url.openConnection() 方法,我們得到了 URLConnection 例項。使用 getInputStream(),我們獲取了網站主頁的內容並打印出來。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw/index.htm?language=en#j2se");
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上面的程式,這將產生以下結果:

輸出

<!DOCTYPE html><html lang="en"><head>	<title>Online Tutorials, Courses, and eBooks ....</body></html>

示例 3

以下示例演示瞭如何使用 Java URLConnection 的 getInputStream() 方法處理 google 的有效 url(使用 http 協議)。在這個例子中,我們建立了一個 URL 類的例項。使用 url.openConnection() 方法,我們得到了 URLConnection 例項。使用 getInputStream(),我們獲取了網站主頁的內容並打印出來。

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上面的程式,這將產生以下結果:

輸出

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" ....</body></html>
java_urlconnection.htm
廣告
© . All rights reserved.