import?java.io.*;
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),會(huì)昌企業(yè)網(wǎng)站建設(shè),會(huì)昌品牌網(wǎng)站建設(shè),網(wǎng)站定制,會(huì)昌網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,會(huì)昌網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
import?java.net.*;
public?class?Demo?{
public?static?void?main(String[]?args)?throws?Exception?{
BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(System.in));
System.out.println("請(qǐng)輸入要顯示源碼的地址:");
URL?url?=?new?URL(br.readLine());
URLConnection?conn?=?url.openConnection();
InputStream?is?=?conn.getInputStream();
byte[]?b?=?new?byte[1024];
int?len?=?0;
while((len?=?is.read(b))!=-1){
System.out.println(new?String(b,0,len,"UTF-8"));
}
}
}
//下班,控制臺(tái)的,網(wǎng)頁(yè)的你稍微修改一下就OK啦。
看不了。java代碼都是在服務(wù)器上執(zhí)行完后,把執(zhí)行完畢的結(jié)果發(fā)送到你的瀏覽器。
你只能看到執(zhí)行后的結(jié)果
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
String urlString;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest(網(wǎng)址);
client.run();
}
public HttpTest(String urlString) {
this.urlString = urlString;
}
public void run() throws Exception {
//生成一個(gè)URL對(duì)象
URL url = new URL(urlString);
//打開(kāi)URL
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//得到輸入流,即獲得了網(wǎng)頁(yè)的內(nèi)容
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
.getInputStream()));
String line;
// 讀取輸入流的數(shù)據(jù),并顯示
while ((line = reader.readLine()) != null){
System.out.println(line);
}
}
}
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ù)鏈接(字符串格式),生成一個(gè)URL對(duì)象
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打開(kāi)URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網(wǎng)頁(yè)的內(nèi)容
String line; // 讀取輸入流的數(shù)據(jù),并顯示
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
根據(jù)具體問(wèn)題類(lèi)型,進(jìn)行步驟拆解/原因原理分析/內(nèi)容拓展等。
具體步驟如下:/導(dǎo)致這種情況的原因主要是……
1.編寫(xiě)useSourceViewer 類(lèi)的基本框架,該類(lèi)僅包括無(wú)返回值的main ()方法,該方法從參數(shù)中獲取URL,通過(guò)輸入緩沖和輸出緩沖將該URL 原碼輸出。
2.編寫(xiě)useSourceViewer 類(lèi),代碼如下:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length 0)
{
try
{
//讀入U(xiǎn)RL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 為增加性能存儲(chǔ)輸入流
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}