「javaurl类型」java urldecode

博主:adminadmin 2023-03-20 05:15:06 794

本篇文章给大家谈谈javaurl类型,以及java urldecode对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java构造方法URL(URL urlobj,String urlSpecifier)是什么意思?

有参构造:

意思是你使用这个构造方法创建对象的时候需要给他传递两个参数。一个参数是URL类型(对象本身的类型),另一个字符串类型。列如 URL rul = new URL(new URL(),"str");

源码:

public URL(URL context, String spec) throws MalformedURLException {

this(context, spec, null);

}

两个参数的构造里调用了三个参数的构造。

具体三参数的构造可以参考源码

/**

* Creates a URL by parsing the given spec with the specified handler

* within a specified context. If the handler is null, the parsing

* occurs as with the two argument constructor.

*

* @param context the context in which to parse the specification.

* @param spec the {@code String} to parse as a URL.

* @param handler the stream handler for the URL.

* @exception MalformedURLException if no protocol is specified, or an

* unknown protocol is found, or {@code spec} is {@code null}.

* @exception SecurityException

* if a security manager exists and its

* {@code checkPermission} method doesn't allow

* specifying a stream handler.

* @see java.net.URL#URL(java.lang.String, java.lang.String,

* int, java.lang.String)

* @see java.net.URLStreamHandler

* @see java.net.URLStreamHandler#parseURL(java.net.URL,

* java.lang.String, int, int)

*/

public URL(URL context, String spec, URLStreamHandler handler)

throws MalformedURLException

{

String original = spec;

int i, limit, c;

int start = 0;

String newProtocol = null;

boolean aRef=false;

boolean isRelative = false;

// Check for permission to specify a handler

if (handler != null) {

SecurityManager sm = System.getSecurityManager();

if (sm != null) {

checkSpecifyHandler(sm);

}

}

try {

limit = spec.length();

while ((limit 0) (spec.charAt(limit - 1) = ' ')) {

limit--; //eliminate trailing whitespace

}

while ((start limit) (spec.charAt(start) = ' ')) {

start++; // eliminate leading whitespace

}

if (spec.regionMatches(true, start, "url:", 0, 4)) {

start += 4;

}

if (start spec.length() spec.charAt(start) == '#') {

/* we're assuming this is a ref relative to the context URL.

* This means protocols cannot start w/ '#', but we must parse

* ref URL's like: "hello:there" w/ a ':' in them.

*/

aRef=true;

}

for (i = start ; !aRef (i limit)

((c = spec.charAt(i)) != '/') ; i++) {

if (c == ':') {

String s = spec.substring(start, i).toLowerCase();

if (isValidProtocol(s)) {

newProtocol = s;

start = i + 1;

}

break;

}

}

// Only use our context if the protocols match.

protocol = newProtocol;

if ((context != null) ((newProtocol == null) ||

newProtocol.equalsIgnoreCase(context.protocol))) {

// inherit the protocol handler from the context

// if not specified to the constructor

if (handler == null) {

handler = context.handler;

}

// If the context is a hierarchical URL scheme and the spec

// contains a matching scheme then maintain backwards

// compatibility and treat it as if the spec didn't contain

// the scheme; see 5.2.3 of RFC2396

if (context.path != null context.path.startsWith("/"))

newProtocol = null;

if (newProtocol == null) {

protocol = context.protocol;

authority = context.authority;

userInfo = context.userInfo;

host = context.host;

port = context.port;

file = context.file;

path = context.path;

isRelative = true;

}

}

if (protocol == null) {

throw new MalformedURLException("no protocol: "+original);

}

// Get the protocol handler if not specified or the protocol

// of the context could not be used

if (handler == null

(handler = getURLStreamHandler(protocol)) == null) {

throw new MalformedURLException("unknown protocol: "+protocol);

}

this.handler = handler;

i = spec.indexOf('#', start);

if (i = 0) {

ref = spec.substring(i + 1, limit);

limit = i;

}

/*

* Handle special case inheritance of query and fragment

* implied by RFC2396 section 5.2.2.

*/

if (isRelative start == limit) {

query = context.query;

if (ref == null) {

ref = context.ref;

}

}

handler.parseURL(this, spec, start, limit);

} catch(MalformedURLException e) {

throw e;

} catch(Exception e) {

MalformedURLException exception = new MalformedURLException(e.getMessage());

exception.initCause(e);

throw exception;

}

}

java中如何实现URL类?

import java.io.*;

import java.net.*;

public class URLTest

{

public static void main(String[] args)

{

try

{

URL url=new URL("");//创建资源类型

String protocol=url.getProtocol();//获取资源类型

String host=url.getHost();//获取域名

int port=url.getPort();//获取端口

String file=url.getFile();//获取路径

System.out.println("url地址的资源类型为:"+protocol+"域名为:"+host+"端口为:"+port+"路径为:"+file);

InputStream is=url.openStream();//获取页面信息流

BufferedReader bfr=new BufferedReader(new InputStreamReader(is));//封装成字符流

String len;

while((len=bfr.readLine())!=null)

{

System.out.println(len);

}

bfr.close();

is.close();

}

catch(MalformedURLException e)

{

System.out.println("创建URL对象发生异常");

}

catch(IOException e)

{

System.out.println("发生IO操作异常");

}

}

}

java中url和uir有什么区别

URI是URL的一个抽象,它不仅包括了统一资源定位符(URL),还包括了统一资源名(URN).大多数实际应用中使用的URI都是URL,但是许多规范和标准像XML都是用URI来定义的.在java1.4和更新的版本中, URI被java.net.URI 类所表示.这个类与java.net.URL 相比有如下3点重要的区别:

· URI 类只关心资源的标识和对URI的解析.它没有方法来检索它的URI所标识的资源。

· URI 类与URL 类相比,它更能适应相关的规范。

· 一个URI 对象能表示一个相对URI 。URL 类在存放之前,就已经对所有的URI进行了“绝对化”的处理。

看着是不是有点晕?

简单的这么理解吧,URL是统一资源定位符,URI是统一资源标识符,URL是URI的一种,但在Java里面不是一种类型,两个类也没有继承关系,但各自提供了toURL()和toURI()来相互转换,在需要的时候放上相应的类型。

关于javaurl类型和java urldecode的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。