本文目录一览:
- java中如何解析页面传入的url
- 用java获取URL路径时出现非法字符
- java解析出url请求的路径和参数键值对类
- 解析url 得到返回值,java
- java 从一个URL中提取特定子字符串保存
- java如何将字符串转化为URL
java中如何解析页面传入的url
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.length > 1) {
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.length > 1) {
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 Map<String, String> URLRequest(String URL) {
Map<String, String> mapRequest = new HashMap<String, 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.length > 1) {
//正确解析
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
} else {
if (arrSplitEqual[0] != "") {
//只有参数没有值,不加入
mapRequest.put(arrSplitEqual[0], "");
}
}
}
return mapRequest;
}
}
测试类:
public class TestCRequest {
/**
* 用于测试CRequest类
* @param args
*/
public static void main(String[] args) {
// 请求url
String str = "index.jsp?Action=del&id=123&sort=";
//url页面路径
System.out.println(CRequest.UrlPage(str));
//url参数键值对
String strRequestKeyAndValues = "";
Map<String, 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"));
}
}
用java获取URL路径时出现非法字符
经常会在浏览器的地址栏里看到这样的字符串%E6%96%87%E6%A1%A3
,这就是被编码后的字符串,下面讨论一下Java的URL编码与解码问题。
方法
java.net.URLDecoder.decode(String s, String enc);
将application/x-www-form-urlencoded字符串转换成普通字符串。java.net.URLEncoder.encode(String s, String enc);
将普通字符串转换成application/x-www-form-urlencoded字符串。
示例代码
import java.net.URLDecoder;
import java.net.URLEncoder;
public class URLDecoderTest {
public static void main(String[] args) throws Exception {
// 将application/x-www-form-urlencoded字符串转换成普通字符串
String keyWord = URLDecoder.decode("%E6%96%87%E6%A1%A3", "gb2312");
System.out.println(keyWord);
// 将普通字符串转换成application/x-www-form-urlencoded字符串
String urlStr = URLEncoder.encode("文档", "gb2312");
System.out.println(urlStr);
}
}
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.length > 1) {
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.length > 1) {
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 Map<String, String> URLRequest(String URL) {
Map<String, String> mapRequest = new HashMap<String, 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.length > 1) {
//正确解析
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=del&id=123&sort=";
//url页面路径
System.out.println(CRequest.UrlPage(str));
//url参数键值对
String strRequestKeyAndValues = "";
Map<String, 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"));
}
}
测试代码运行效果
index.jsp
key:id,Value:123;key:sort,Value:;key:action,Value:del;
null
解析url 得到返回值,java
try {
URL url = new URL("http://ip.taobao.com/service/getIpInfo2.php?ip=" + ip);
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ((rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);
}
byte[] b = outStream.toByteArray();
//关闭
outStream.close();
is.close();
connect.disconnect();
String address = new String(b);
if (address.indexOf("省") != -1) {
ipprovince = address.substring(0, address.indexOf("省") + 1);
System.out.println("省地址为:" + ipprovince);
} else if (address.indexOf("区") != -1) {
ipprovince = address.substring(0, address.indexOf("区") + 1);
System.out.println("省地址为:" + ipprovince);
} else {
ipprovince = address.substring(0, address.indexOf("市") + 1);
System.out.println("省地址为:" + ipprovince);
}
} catch (Exception e) {
e.printStackTrace();
}
java 从一个URL中提取特定子字符串保存
public class Test {
public static void main(String[] args) {
String url = "http://www.example.com/path/to/resource";
//首先去掉开头部分如:ftp://,http://等
url = url.substring(url.indexOf("//") >= 0 ? (url.indexOf("//") + 2) : 0);
//查找出一个/出现的地方
int index = url.indexOf("/") > 0 ? url.indexOf("/") : url.length();
//前面部分
String before = url.substring(0, index);
//后面部分
String end;
if (index == url.length()) //说明没有后面部分
end = "";
else
end = url.substring(index + 1);
System.out.println(url);
System.out.println(before);
System.out.println(end);
}
}
java如何将字符串转化为URL
将字符串转换成URL可以使用创建一个URL对象,并将字符串赋给这个URL对象。
示例代码
String str = "填写字符串的链接地址";
try {
URL url = new URL(str);
} catch (MalformedURLException e) {
e.printStackTrace();
}
注意,创建URL对象会有异常,所以使用try处理抛出的异常。