?? urlconnectiondemo.java
字號:
/*【例11-3】 調用URL對象的openConnection()方法創建URLConnection對象,
* 實現與服務器的數據發送與接收。
*/
//程序清單11-3: URLConnectionDemo.java
package url;
import java.net.*;
import java.io.*;
import java.util.Date;
public class URLConnectionDemo {
public static void main(String[] args) throws MalformedURLException,
IOException {
// 建立指向網絡中cgi的URL對象
URL url = new URL("http://localhost/ch11/test.cgi");
// 使用url對象的openConnection()方法,來獲取URLConnection類的對象
URLConnection urlConn = url.openConnection();
// 使用urlConn對象的getContentType()方法獲取資源文件的類型,并顯示
System.out.println("ContentType: " + urlConn.getContentType());
// 使用urlConn對象的getContentLength()方法獲取資源文件的長度
System.out.println("ContentLength: " + urlConn.getContentLength());
// 使用urlConn對象的getDate()方法獲取資源文件創建的時間
System.out.println("Date: " + new Date(urlConn.getDate()));
// 使用urlConn對象的getLastModified()方法獲取資源文件最后一次被修改的時間
System.out.println("LastModified: "
+ new Date(urlConn.getLastModified()));
// 向服務器輸出數據
urlConn.setDoOutput(true);
PrintStream outps = new PrintStream(urlConn.getOutputStream());
outps.println("ABCDEFGHIJKL");
outps.close();
// 從服務器讀取數據
DataInputStream indis = new DataInputStream(urlConn.getInputStream());
String inputLine;
while ((inputLine = indis.readLine()) != null) {
System.out.println(inputLine);
}
indis.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -