简体   繁体   English

实用程序从URL获取路径

[英]Utils to get path from URL

I need to get the path after the domain name. 我需要获取域名后的路径。
Ex: The url is https://vaadin.com:8080/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up 范例:网址为https://vaadin.com:8080/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up
I want to get /wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up . 我想获取/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up Is there any Utils class for java to do this quickly? 是否有Java的Utils类可快速执行此操作? Thanks so much! 非常感谢!

Why don't you just use getPath() of URL class? 为什么不只使用URL类的getPath() see here: 看这里:

http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#getPath%28%29 http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#getPath%28%29

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. 类URL表示一个统一资源定位符,它是指向万维网上“资源”的指针。 A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine. 资源可以是简单的文件或目录,也可以是对更复杂对象的引用,例如对数据库或搜索引擎的查询。 More information on the types of URLs and their formats can be found at: 有关URL类型及其格式的更多信息,请参见:

 http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html 

In general, a URL can be broken into several parts. 通常,URL可以分为几个部分。 The previous example of a URL indicates that the protocol to use is http (HyperText Transfer Protocol) and that the information resides on a host machine named www.socs.uts.edu.au. URL的前面的示例指示要使用的协议为http(超文本传输​​协议),并且该信息位于名为www.socs.uts.edu.au的主机上。 The information on that host machine is named /MosaicDocs-old/url-primer.html. 该主机上的信息称为/MosaicDocs-old/url-primer.html。 The exact meaning of this name on the host machine is both protocol dependent and host dependent. 此名称在主机上的确切含义既取决于协议,也取决于主机。 The information normally resides in a file, but it could be generated on the fly. 该信息通常位于文件中,但是可以即时生成。 This component of the URL is called the path component. URL的此组件称为路径组件。

As @Yan Foto suggested the best way is to use java.net.URL class. 正如@Yan Foto所建议的那样,最好的方法是使用java.net.URL类。

Example: 例:

URL url = new URL("https://vaadin.com:8080/wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up");

System.out.print("Path: " + url.getPath());

It prints following output : 打印以下输出:

Path: wiki/-/wiki/Main/Authenticating+Vaadin-based+applications/pop_up

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM