📄 webpath.java
字号:
package downLoadImage;
import java.net.*;
import java.util.regex.*;
class WebPath {
private String web="";
protected URL website;
String urlPath;
//private static String regex_1="^(http://)[^\\s]+";
// private static String regex_2="^(/|[^(\\.|\\s)])[^\\s]+";
// private static String regex_3="^(\\.\\./)(\\.\\./)*[^\\s]+";
private static String regex_4="[^/]+/(\\.\\./)";
public WebPath(String p,String w){
urlPath=p;
web=w;
trimPath();
}
public WebPath(String p,URL w){
urlPath=p;website=w;
trimPath();
}
public URL getURL(){
/*
try{
if(Pattern.matches(regex_1, urlPath))
return new URL(urlPath);
else if(Pattern.matches(regex_2, urlPath)){
return new URL( web+(urlPath.charAt(0)=='/'?urlPath.substring(1, urlPath.length()):urlPath) );
}else if(Pattern.matches(regex_3, urlPath)){
return new URL(getdefinitePath(web+urlPath));
}
}catch(MalformedURLException e){
return null;
}
return null;
*/
try{
if(urlPath.startsWith("http"))
return new URL(urlPath);
URL cur=new URL(website,urlPath);
return cur;
}catch (MalformedURLException me) {
System.out.println("没有找到网站");
return null;
}
}
private void trimPath(){
if(urlPath.startsWith("/"))
urlPath=urlPath.substring(1, urlPath.length());
}
public String getURLToString(){
/*
if(Pattern.matches(regex_1, urlPath)){
return urlPath;
}
else if(Pattern.matches(regex_2, urlPath)){
return web+(urlPath.charAt(0)=='/'?urlPath.substring(1, urlPath.length()):urlPath);
}else if(Pattern.matches(regex_3, urlPath)){
return getdefinitePath(web+urlPath);
}else{
return null;
}
*/
if(urlPath.startsWith("http"))
return urlPath;
try{
URL cur=new URL(website,urlPath);
return cur.toString();
}catch (MalformedURLException me) {
System.out.println("没有找到网站");
return null;
}
}
public String getParent(){
String s=getdefinitePath(web+urlPath);
return s.substring(0, s.lastIndexOf("/")+1);
}
private String getdefinitePath(String s){
Pattern p=Pattern.compile(regex_4, Pattern.CASE_INSENSITIVE);
Matcher m=p.matcher(s);
if(m.find()){
StringBuffer sb=new StringBuffer();
m.appendReplacement(sb, "");
m.appendTail(sb);
return getdefinitePath(sb.toString());
}else{
return s;
}
}
public String getCurrentURL(){
if (urlPath==null)
return null;
else{
char c;
int directory=urlPath.lastIndexOf("/");
if(directory<0)
return WebInfo.Prefix;
StringBuffer sb=new StringBuffer();
for(int i=0;i<directory;i++){
c=urlPath.charAt(i);
if(c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9')
sb.append(c);
}
return sb.toString();
}
}
/*
public static void main(String s[]){
String str="../av/index.htm";
WebPath wp=new WebPath(str);
wp.setWeb("http://www.freesummer.cn/abc/aa/");
System.out.println(wp.getURLToString());
System.out.println(wp.getParent());
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -