Could you share a link to an URL parsing implementation?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 11:04:56

The URL class gives you everything you need. See http://download.oracle.com/javase/6/docs/api/java/net/URL.html

URL url = new URL("protocol://user:password@host:port/path/document?arg1=val1&arg2=val2#part");
url.getProtocol();
url.getUserInfo();
url.getAuthority();
url.getHost();
url.getPort();
url.getPath(); // document part is contained within the path field
url.getQuery();
url.getRef(); // gets #part

Use the java.net.URI class for this. URLs are for real resources and real protocols. URIs are for possibly non-existent protocols and resources.

In Java, just use the URL class. It provides methods such as getProtocol, getHost, etc. to obtain the different parts of the URL.

URL does not support ldap by default. One can extend URL and add protocols, but I ended up with a simple parser and a small new class.

Based on @Codemwnci answer, here's a full example to get the filename from a url with or without arguments:

URL videoUrl = new URL("https://somesite.com/path/v/t43.1792-2/1186696120_n.mp4?efg=something");
String videoFileName = videoUrl.getPath().substring(videoUrl.getPath().lastIndexOf("/") + 1);

1186696120_n.mp4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!