Java URL
URLUniform Resource Locator FTP
Java URL URL
protocol://host:port/path?query#fragment
protocol() HTTPHTTPSFTP Fileport path
HTTP URL :
http://www.runoob.com/index.html?language=cn#j2se
URL
- (protocol)http
- (host:port)www.runoob.com
- (port): 80 URL HTTP 80
- (path)/index.html
- (query)language=cn
- (fragment)j2se id j2se HTML
URL
java.netURLURLURL
java.net.URLURLjava.net.URL
| 1 | public URL(String protocol, String host, int port, String file) throws MalformedURLException. ()URL |
| 2 | public URL(String protocol, String host, String file) throws MalformedURLException URL |
| 3 | public URL(String url) throws MalformedURLException URLURL |
| 4 | public URL(URL context, String url) throws MalformedURLException URL |
URLURL
| 1 | public String getPath() URL |
| 2 | public String getQuery() URL |
| 3 | public String getAuthority() URL |
| 4 | public int getPort() URL |
| 5 | public int getDefaultPort() |
| 6 | public String getProtocol() URL |
| 7 | public String getHost() URL |
| 8 | public String getFile() URL |
| 9 | public String getRef() URL "" |
| 10 | public URLConnection openConnection() throws IOException URL |
java.netURLURL
URLDemo.java
import java.net.*;
import java.io.*;
public class URLDemo
{
public static void main(String [] args)
{
try
{
URL url = new URL("http://www.runoob.com/index.html?language=cn#j2se");
System.out.println("URL " + url.toString());
System.out.println("" + url.getProtocol());
System.out.println("" + url.getAuthority());
System.out.println("" + url.getFile());
System.out.println("" + url.getHost());
System.out.println("" + url.getPath());
System.out.println("" + url.getPort());
System.out.println("" + url.getDefaultPort());
System.out.println("" + url.getQuery());
System.out.println("" + url.getRef());
}catch(IOException e)
{
e.printStackTrace();
}
}
}
:
URL http://www.runoob.com/index.html?language=cn#j2se http www.runoob.com /index.html?language=cn www.runoob.com /index.html -1 80 language=cn j2se
URLConnections
openConnection() java.net.URLConnection
HTTPURL, openConnection() HttpURLConnection
URL JAR , openConnection() JarURLConnection
...
URLConnection
| 1 | Object getContent() URL |
| 2 | Object getContent(Class[] classes) URL |
| 3 | String getContentEncoding() content-encoding |
| 4 | int getContentLength() content-length |
| 5 | String getContentType() content-type |
| 6 | int getLastModified() last-modified |
| 7 | long getExpiration() expires |
| 8 | long getIfModifiedSince() ifModifiedSince |
| 9 | public void setDoInput(boolean input) URL / URL DoInput true false true |
| 10 | public void setDoOutput(boolean output) URL / URL DoOutput true false false |
| 11 | public InputStream getInputStream() throws IOException URL |
| 12 | public OutputStream getOutputStream() throws IOException URL, |
| 13 | public URL getURL() URLConnection URL |
URLHTTP openConnection HttpURLConnection
URLConnDemo.java
import java.net.*;
import java.io.*;
public class URLConnDemo
{
public static void main(String [] args)
{
try
{
URL url = new URL("http://www.runoob.com");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection)
{
connection = (HttpURLConnection) urlConnection;
}
else
{
System.out.println(" URL ");
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null)
{
urlString += current;
}
System.out.println(urlString);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
$ javac URLConnDemo.java $ java URLConnDemo .....(http://www.runoob.com) HTML .....
VEGETAZ
veg***[email protected]
URL openConnection HttpURLConnection Unicode u+
// InputStreamStringBuilder StringBuilder content = new StringBuilder(); // // Unicode StringBuilder converted = new StringBuilder(); // for(int i=0; i<content.length(); i++){ // uunicode if(i<content.length()-1 && content.substring(i, i+2).equals("\\u")){ // ui i+=2; // String code = content.substring(i, i+4); // 16Char converted.append( (char)Integer.parseInt(code, 16) ); // i+4-1 i+=3; } // else converted.append( content.charAt(i) ); } System.out.println("\n" + converted);VEGETAZ
veg***[email protected]