您的位置:

java获取url,java获取url后面的参数

本文目录一览:

java获取URL

import java.io.BufferedReader;

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路径?

1、利用System.getProperty()函数获取当前路径:

System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径

2、使用File提供的函数获取当前路径:

File directory = new File("");//设定为当前文件夹

try{

System.out.println(directory.getCanonicalPath());//获取标准的路径

System.out.println(directory.getAbsolutePath());//获取绝对路径

}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(".")和new File("..")两种路径有所区别。

# 对于getCanonicalPath()函数,“."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹

# 对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径

# 至于getPath()函数,得到的只是你在new File()时设定的路径

java怎样获取url参数

如果是javaweb 项目,那么非常简单,直接调用 HttpServletRequest 对象的 .getParamter("参数名称") 方法即可得到。

如果是普通java 项目:

/**

* 获取网址的指定参数值

*

* @param url

* 网址

* @param parameter

* 参数名称

* @author cevencheng

* @return

*/

public static String getParameter(String url, String parameter, String defaultValue) {

try {

final String charset = "utf-8";

url = URLDecoder.decode(url, charset);

if (url.indexOf('?') != -1) {

final String contents = url.substring(url.indexOf('?') + 1);

HashMapString, String map = new HashMapString, String();

String[] keyValues = contents.split("");

for (int i = 0; i keyValues.length; i++) {

String key = keyValues[i].substring(0, keyValues[i].indexOf("="));

String value = keyValues[i].substring(keyValues[i].indexOf("=") + 1);

if (key.equals(parameter)) {

if (value == null || "".equals(value.trim())) {

return defaultValue;

}

return value;

}

map.put(key, value);

}

}

return null;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

java怎么获取url上的参数

解析url,本想用正则表达式处理,但正则表达式速度较慢。用split处理一下就可以了。

package RequestPackage;

import java.util.HashMap;

import java.util.Map;

public class CRequest {

/**

* 解析出url请求的路径,包括页面

* @param strURL url地址

* @return url路径

*/

public static String UrlPage(String strURL)

{

String strPage=null;

String[] arrSplit=null;

strURL=strURL.trim().toLowerCase();

arrSplit=strURL.split("[?]");

if(strURL.length()0)

{

if(arrSplit.length1)

{

if(arrSplit[0]!=null)

{

strPage=arrSplit[0];

}

}

}

return strPage;

}

/**

* 去掉url中的路径,留下请求参数部分

* @param strURL url地址

* @return url请求参数部分

*/

private static String TruncateUrlPage(String strURL)

{

String strAllParam=null;

String[] arrSplit=null;

strURL=strURL.trim().toLowerCase();

arrSplit=strURL.split("[?]");

if(strURL.length()1)

{

if(arrSplit.length1)

{

if(arrSplit[1]!=null)

{

strAllParam=arrSplit[1];

}

}

}

return strAllParam;

}

/**

* 解析出url参数中的键值对

* 如 "index.jsp?Action=delid=123",解析出Action:del,id:123存入map中

* @param URL url地址

* @return url请求参数部分

*/

public static MapString, String URLRequest(String URL)

{

MapString, String mapRequest = new HashMapString, String();

String[] arrSplit=null;

String strUrlParam=TruncateUrlPage(URL);

if(strUrlParam==null)

{

return mapRequest;

}

//每个键值为一组

arrSplit=strUrlParam.split("[]");

for(String strSplit:arrSplit)

{

String[] arrSplitEqual=null;

arrSplitEqual= strSplit.split("[=]");

//解析出键值

if(arrSplitEqual.length1)

{

//正确解析

mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

}

else

{

if(arrSplitEqual[0]!="")

{

//只有参数没有值,不加入

mapRequest.put(arrSplitEqual[0], "");

}

}

}

return mapRequest;

}

}

测试类

package RequestPackage;

import java.util.Map;

public class TestCRequest {

/**用于测试CRequest类

* @param args

*/

public static void main(String[] args) {

// 请求url

String str = "index.jsp?Action=delid=123sort=";

//url页面路径

System.out.println(CRequest.UrlPage(str));

//url参数键值对

String strRequestKeyAndValues="";

MapString, String mapRequest = CRequest.URLRequest(str);

for(String strRequestKey: mapRequest.keySet()) {

String strRequestValue=mapRequest.get(strRequestKey);

strRequestKeyAndValues+="key:"+strRequestKey+",Value:"+strRequestValue+";";

}

System.out.println(strRequestKeyAndValues);

//获取无效键时,输出null

System.out.println(mapRequest.get("page"));

}

}