快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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解码
文章URL:http://6mz.cn/article/ddspppc.html

其他资讯