资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java解析url代码 java中urlencode解码

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

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

10年积累的成都网站建设、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有太谷免费网站建设让你可以放心的选择与我们合作。

2.编写useSourceViewer 类,代码如下:

import java点虐 .*;

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}

解析URL中的汉字参数 Java

import java点虐 .URLDecoder;

import java点虐 .URLEncoder;

String strTest = "?=abc?中%123,4";

strTest = URLEncoder.encode(strTest, "UTF-8");

System.out.println(strTest);

strTest = URLDecoder.decode(strTest,"UTF-8");

System.out.println(strTest);

java获取URL

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java点虐 .MalformedURLException;

import java点虐 .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点虐 .URL url = new java点虐 .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代码 java中urlencode解码
转载源于:http://cdkjz.cn/article/ddspppc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220