成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java社交網(wǎng)站源代碼 javaweb源碼分享網(wǎng)站

Java訪問指定URL并獲取網(wǎng)頁源代碼

1.編寫useSourceViewer 類的基本框架,該類僅包括無返回值的main ()方法,該方法從參數(shù)中獲取URL,通過輸入緩沖和輸出緩沖將該URL 原碼輸出。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站設(shè)計、魏都網(wǎng)絡(luò)推廣、微信小程序開發(fā)、魏都網(wǎng)絡(luò)營銷、魏都企業(yè)策劃、魏都品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供魏都建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:jinyejixie.com

2.編寫useSourceViewer 類,代碼如下:

import java.net.*;

import java.io.*;

public class useSourceViewer

{

public static void main (String[] args)

{

if (args.length 0)

{

try

{

//讀入URL

URL u = new URL(args[0]);

InputStream in = u.openStream( );

// 為增加性能存儲輸入流

in = new BufferedInputStream(in);

// 將輸入流連接到閱讀器

Reader r = new InputStreamReader(in);

int c;

while ((c = r.read( )) != -1)

{

System.out.print((char) c);

}

Object o = u.getContent( );

System.out.println("I got a " + o.getClass().getName( ));

}

catch (MalformedURLException e)

{

System.err.println(args[0] + " is not a parseable URL");

}

catch (IOException e)

{

System.err.println(e);

}

} // end if

} // end main

} // end SourceViewer}

java 聊天室 源代碼

【ClientSocketDemo.java 客戶端Java源代碼】

import java.net.*;

import java.io.*;

public class ClientSocketDemo

{

//聲明客戶端Socket對象socket

Socket socket = null;

//聲明客戶器端數(shù)據(jù)輸入輸出流

DataInputStream in;

DataOutputStream out;

//聲明字符串?dāng)?shù)組對象response,用于存儲從服務(wù)器接收到的信息

String response[];

//執(zhí)行過程中,沒有參數(shù)時的構(gòu)造方法,本地服務(wù)器在本地,取默認(rèn)端口10745

public ClientSocketDemo()

{

try

{

//創(chuàng)建客戶端socket,服務(wù)器地址取本地,端口號為10745

socket = new Socket("localhost",10745);

//創(chuàng)建客戶端數(shù)據(jù)輸入輸出流,用于對服務(wù)器端發(fā)送或接收數(shù)據(jù)

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//獲取客戶端地址及端口號

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

//向服務(wù)器發(fā)送數(shù)據(jù)

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

//從服務(wù)器接收數(shù)據(jù)

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//執(zhí)行過程中,有一個參數(shù)時的構(gòu)造方法,參數(shù)指定服務(wù)器地址,取默認(rèn)端口10745

public ClientSocketDemo(String hostname)

{

try

{

//創(chuàng)建客戶端socket,hostname參數(shù)指定服務(wù)器地址,端口號為10745

socket = new Socket(hostname,10745);

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//執(zhí)行過程中,有兩個個參數(shù)時的構(gòu)造方法,第一個參數(shù)hostname指定服務(wù)器地址

//第一個參數(shù)serverPort指定服務(wù)器端口號

public ClientSocketDemo(String hostname,String serverPort)

{

try

{

socket = new Socket(hostname,Integer.parseInt(serverPort));

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

String comd[] = args;

if(comd.length == 0)

{

System.out.println("Use localhost(127.0.0.1) and default port");

ClientSocketDemo demo = new ClientSocketDemo();

}

else if(comd.length == 1)

{

System.out.println("Use default port");

ClientSocketDemo demo = new ClientSocketDemo(args[0]);

}

else if(comd.length == 2)

{

System.out.println("Hostname and port are named by user");

ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);

}

else System.out.println("ERROR");

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

【ServerSocketDemo.java 服務(wù)器端Java源代碼】

import java.net.*;

import java.io.*;

public class ServerSocketDemo

{

//聲明ServerSocket類對象

ServerSocket serverSocket;

//聲明并初始化服務(wù)器端監(jiān)聽端口號常量

public static final int PORT = 10745;

//聲明服務(wù)器端數(shù)據(jù)輸入輸出流

DataInputStream in;

DataOutputStream out;

//聲明InetAddress類對象ip,用于獲取服務(wù)器地址及端口號等信息

InetAddress ip = null;

//聲明字符串?dāng)?shù)組對象request,用于存儲從客戶端發(fā)送來的信息

String request[];

public ServerSocketDemo()

{

request = new String[3]; //初始化字符串?dāng)?shù)組

try

{

//獲取本地服務(wù)器地址信息

ip = InetAddress.getLocalHost();

//以PORT為服務(wù)端口號,創(chuàng)建serverSocket對象以監(jiān)聽該端口上的連接

serverSocket = new ServerSocket(PORT);

//創(chuàng)建Socket類的對象socket,用于保存連接到服務(wù)器的客戶端socket對象

Socket socket = serverSocket.accept();

System.out.println("This is server:"+String.valueOf(ip)+PORT);

//創(chuàng)建服務(wù)器端數(shù)據(jù)輸入輸出流,用于對客戶端接收或發(fā)送數(shù)據(jù)

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//接收客戶端發(fā)送來的數(shù)據(jù)信息,并顯示

request[0] = in.readUTF();

request[1] = in.readUTF();

request[2] = in.readUTF();

System.out.println("Received messages form client is:");

System.out.println(request[0]);

System.out.println(request[1]);

System.out.println(request[2]);

//向客戶端發(fā)送數(shù)據(jù)

out.writeUTF("Hello client!");

out.writeUTF("Your ip is:"+request[1]);

out.writeUTF("Your port is:"+request[2]);

}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

ServerSocketDemo demo = new ServerSocketDemo();

}

}

java中如何根據(jù)一個網(wǎng)址獲得該網(wǎng)頁的源代碼?

package test;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpTest {

private String u;

private String encoding;

public static void main(String[] args) throws Exception {

HttpTest client = new HttpTest("", "UTF-8");

client.run();

}

public HttpTest(String u, String encoding) {

this.u = u;

this.encoding = encoding;

}

public void run() throws Exception {

URL url = new URL(u);// 根據(jù)鏈接(字符串格式),生成一個URL對象

HttpURLConnection urlConnection = (HttpURLConnection) url

.openConnection();// 打開URL

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網(wǎng)頁的內(nèi)容

String line; // 讀取輸入流的數(shù)據(jù),并顯示

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

}

}

根據(jù)具體問題類型,進(jìn)行步驟拆解/原因原理分析/內(nèi)容拓展等。

具體步驟如下:/導(dǎo)致這種情況的原因主要是……

網(wǎng)站欄目:java社交網(wǎng)站源代碼 javaweb源碼分享網(wǎng)站
文章位置:http://jinyejixie.com/article8/dosihop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號營銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)服務(wù)器托管、網(wǎng)站營銷、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司
鄂托克前旗| 大兴区| 尚义县| 定安县| 安塞县| 成安县| 靖州| 汶川县| 叙永县| 辰溪县| 福州市| 晋中市| 萨迦县| 政和县| 丰宁| 纳雍县| 山东省| 满洲里市| 东山县| 日土县| 湖口县| 吴忠市| 广灵县| 青岛市| 许昌县| 兴仁县| 长宁县| 金坛市| 南宫市| 类乌齐县| 静海县| 北海市| 波密县| 清水县| 台东市| 思茅市| 三门峡市| 云浮市| 龙南县| 上虞市| 万全县|