重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java获取url源代码 java获取url文件名

java获取URL

import java.io.BufferedReader;

郾城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class GetLinks {

private String webSource;

private String url;

public GetLinks(String url) throws MalformedURLException, IOException {

this.url = Complete(url);

webSource = getWebCon(this.url);

}

private String getWebCon(String strURL) throws MalformedURLException,

IOException {

StringBuffer sb = new StringBuffer();

java.net.URL url = new java.net.URL(strURL);

BufferedReader in = new BufferedReader(new InputStreamReader(url

.openStream()));

String line;

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

sb.append(line);

}

in.close();

return sb.toString();

}

private String Complete(String link)throws MalformedURLException{

URL url1 = new URL(link);

URL url2 = new URL(link+"/");

String handledUrl = link;

try{

StringBuffer sb1 = new StringBuffer();

BufferedReader in1 = new BufferedReader(new InputStreamReader(url1

.openStream()));

String line1;

while ((line1 = in1.readLine()) != null) {

sb1.append(line1);

}

in1.close();

StringBuffer sb2 = new StringBuffer();

BufferedReader in2 = new BufferedReader(new InputStreamReader(url2

.openStream()));

String line2;

while ((line2 = in2.readLine()) != null) {

sb2.append(line2);

}

in1.close();

if(sb1.toString().equals(sb2.toString())){

handledUrl = link+"/";

}

}catch(Exception e){

handledUrl = link;

}

return handledUrl;

}

/**

* 处理链接的相对路径

* @param link 相对路径或绝对路径

* @return 绝对路径

*/

private String urlHandler(String link) {

if (link == null)

return null;

link = link.trim();

if (link.toLowerCase().startsWith("http://")

|| link.toLowerCase().startsWith("https://")) {

return link;

}

String pare = url.trim();

if (!link.startsWith("/")) {

if (pare.endsWith("/")) {

return pare + link;

}

if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {

return pare + "/" + link;

} else {

int lastSeparatorIndex = url.lastIndexOf("/");

return url.substring(0, lastSeparatorIndex + 1) + link;

}

}else{

if (url.lastIndexOf("/") == url.indexOf("//") + 1 || url.lastIndexOf("/") == url.indexOf("//") + 2) {

return pare + link;

}else{

return url.substring(0,url.indexOf("/", url.indexOf("//")+3)) + link;

}

}

}

public ListString getAnchorTagUrls() {

if (webSource == null) {

System.out.println("没有网页源代码");

return null;

}

ArrayListString list = new ArrayListString();

int index = 0;

while (index != -1) {

index = webSource.toLowerCase().indexOf("a ", index);

if (index != -1) {

int end = webSource.indexOf("", index);

String str = webSource.substring(index, end == -1 ? webSource

.length() : end);

str = str.replaceAll("\\s*=\\s*", "=");

if (str.toLowerCase().matches("^a.*href\\s*=\\s*[\'|\"]?.*")) {// "^a\\s+\\w*\\s*href\\s*=\\s*[\'|\"]?.*"

int hrefIndex = str.toLowerCase().indexOf("href=");

int leadingQuotesIndex = -1;

if ((leadingQuotesIndex = str.indexOf("\"", hrefIndex

+ "href=".length())) != -1) { // 形如a

// href="....."

int TrailingQuotesIndex = str.indexOf("\"",

leadingQuotesIndex + 1);

TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str

.length() : TrailingQuotesIndex;

str = str.substring(leadingQuotesIndex + 1,

TrailingQuotesIndex);

str = urlHandler(str);

list.add(str);

System.out.println(str);

index += "a ".length();

continue;

}

if ((leadingQuotesIndex = str.indexOf("\'", hrefIndex

+ "href=".length())) != -1) { // 形如a

// href='.....'

int TrailingQuotesIndex = str.indexOf("\'",

leadingQuotesIndex + 1);

TrailingQuotesIndex = TrailingQuotesIndex == -1 ? str

.length() : TrailingQuotesIndex;

str = str.substring(leadingQuotesIndex + 1,

TrailingQuotesIndex);

str = urlHandler(str);

System.out.println(str);

list.add(str);

index += "a ".length();

continue;

}

int whitespaceIndex = str.indexOf(" ", hrefIndex

+ "href=".length()); // 形如a href=

//

whitespaceIndex = whitespaceIndex == -1 ? str.length()

: whitespaceIndex;

str = str.substring(hrefIndex + "href=".length(),

whitespaceIndex);

str = urlHandler(str);

list.add(str);

System.out.println(str);

}

index += "a ".length();

}

}

return list;

}

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

GetLinks gl = new GetLinks("");

ListString list = gl.getAnchorTagUrls();

for(String str:list) {

System.out.println(str);

}

}

}

java程序读取一个url页面的源代码

传入一个url,返回源代码; public static String getHTML(String url){// 获取指定URL的网页,返回网页内容的字符串,然后将此字符串存到文件即可 try { URL newUrl = new URL(url); URLConnection connect = newUrl.openConnection(); connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); DataInputStream dis = new DataInputStream(connect.getInputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8")); String html = ""; String readLine = null; while((readLine = in.readLine()) != null) { html = html + readLine; } in.close(); return html; }catch (MalformedURLException me){ System.out.println("MalformedURLException" + me); }catch (IOException ioe){ System.out.println("ioeException" + ioe); } return null; }

Java访问指定URL并获取网页源代码

1.编写useSourceViewer 类的基本框架,该类仅包括无返回值的main ()方法,该方法从参数中获取URL,通过输入缓冲和输出缓冲将该URL 原码输出。

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中如何根据一个网址获得该网页的源代码?

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);// 根据链接(字符串格式),生成一个URL对象

HttpURLConnection urlConnection = (HttpURLConnection) url

.openConnection();// 打开URL

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(), encoding));// 得到输入流,即获得了网页的内容

String line; // 读取输入流的数据,并显示

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

System.out.println(line);

}

}

}

根据具体问题类型,进行步骤拆解/原因原理分析/内容拓展等。

具体步骤如下:/导致这种情况的原因主要是……


网站标题:java获取url源代码 java获取url文件名
URL网址:http://cqcxhl.cn/article/doscghh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP