java截取url
1. java中路径截取方法
截取字符串是用String类中的substring()方法。查API就知道它的用法,如何正确的截取那也得根据项目的实际情况去找它的规律。
如果只是像你说的那个例子我暂时只想到一个笨办法:
"d:\a\b\c\d\e".substring(0, "d:\a\b\c\d\e".substring(0,"d:\a\b\c\d\e".lastIndexOf("\"")).lastIndexOf("\""));
这样截取出来就是"d:\a\b\c"了。 但是这种不灵活,如果你将现有的路径回退三级那就只有再加一个截取的方法了。
当然 你还可以写一个通用的方法,可以给方法传两个参数,一个是路径字符串,另一个就是要退几级,方法里面用循环来实现
如果是在做项目当中需要保存东西大可不必要这样的绝对路径,就将文件保存到项目中的目录就好了,这样的话操作路径比较方便,向上退一级就在路径前面加上"..\"就好了
有可能我讲的你不好理解,这个也是要有实际的操作要好理解一些的,不理解你再问我。
希望对你有所帮助
2. 用java做分页时,如何获取url
httpServletRequest.getRequestURI();
或者
string url = HttpContext.Current.Request.Url.ToString();
//获取当前页的URL
或者
设置参数 前台传参:....&url=.... 后台接值: request.getParameter("url")
java 后台分页 httpServletRequest.getRequestURI();
建议用AJAX,这样可以不需要获取 url
3. java的substring取得url地址
如果域名是固定的http://..com/:
String url = "http://..com/new?word=java%E7%9A%84substring%E5%8F%96%E5%BE%97url%E5%9C%B0%E5%9D%80";
String s = url.substring(0, 24);
4. java 从一个URL中提取特定子字符串保存
public class Test{
public static void main(String[] args){
String url="ftp://dvdmaker.9966.org:1981/bdig/123/dfshio/sfjgo/";
//首先去掉开头部分如:ftp://,http://等
url=url.substring(url.indexOf("//")>=0?(url.indexOf("//")+2):0);
//查找出一个/出现的地方
int index=url.indexOf("/")<0?url.length():url.indexOf("/");
//前面部分
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);
}
}
5. 谁有截取url中 一级域名的 java代码
String url = request.getScheme()+"://"; //请求协议 http 或 https
url+=request.getHeader("host"); // 请求服务器
url+=request.getRequestURI(); // 工程名
if(request.getQueryString()!=null) //判断请求参数是否为空
url+="?"+request.getQueryString(); // 参数
6. java正则表达式截取URL
Stringreg=".*\/\/([^\/\:]*).*";
Stringstr1="http://192.168.1.6:9999/caservice/webservice/caServiceWs?wsdl";
System.out.println(str1.replaceAll(reg,"$1"));
Stringstr2="http://192.168.1.6/caservice/webservice/caServiceWs?wsdl";
System.out.println(str2.replaceAll(reg,"$1"));
7. java如何提取url里的域名
java.net.URL 而不是 java.Net.URL
使用getHost获取到的域名信息存在安全漏洞,例如:http://127.0.0.1\.123.cn/1.htm这个URL使用getHost得到的域名是127.0.0.1\.123.cn。
建议使用正则表达式
8. 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 List<String> getAnchorTagUrls() {
if (webSource == null) {
System.out.println("没有网页源代码");
return null;
}
ArrayList<String> list = new ArrayList<String>();
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=
// http://www..com >
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("http://www..com");
List<String> list = gl.getAnchorTagUrls();
for(String str:list) {
System.out.println(str);
}
}
}
9. 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 List<String> getAnchorTagUrls() {
if (webSource == null) {
System.out.println("没有网页源代码");
return null;
}
ArrayList<String> list = new ArrayList<String>();
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=
// http://www..com >
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("http://www..com");
List<String> list = gl.getAnchorTagUrls();
for(String str:list) {
System.out.println(str);
}
}
}
10. java 字符串 对URL的截取 求 实现类似JS 中 location.pathname的方法
查询 ‘?’ 在url中的位置,可能有两种情况
1.未找到,此时整个url就是 location.pathname
2.找到了。假设位置为index, 那么url.substring(0,index) 就是 location.pathname