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