URLクラスがサポートするプロトコルの状況
3504 ワード
簡単なプログラムでjavaを見てみましょう.net.URLクラスは、いくつかのプロトコルのサポート状況に対して使用されます.
package com.lr.testNetAddress;
import java.net.MalformedURLException;
import java.net.URL;
public class testProtocol {
private static void protcol(String str) {
try {
URL url = new URL(str) ;
System.out.println(" : "+url.getProtocol()) ;
} catch (MalformedURLException e) {
System.out.println(" : "+str.substring(0,str.indexOf(":")));
}
}
public static void main(String[] args) {
String[] urls = {
"http://www.abc.com",//
"https://www.abc.com",// http
"ftp://www.abc.com",//
"mailto:[email protected]",//
"telnet://www.abc.com",//telnet
"file://C:\\Documents and Settings\\Administrator\\ \\",//
"gopher://gopher.abc.com",//gopher
"jar:http://www.abc.com",//JAR
"nfs://www.abc.com",//
"ldap://www.abc.com",//
"jdbc:mysql://www.abc.com:3306/hello",//jdbc
"rmi://www.abc.com",//
"doc://www.abc.com",//
"netdoc://www.abc.com",//
"systemresource://www.abc.com",//
"verbatim:http://www.abc.com",//
} ;
for(String url : urls){
protcol(url) ;
}
}
}
結果:
: http
: https
: ftp
: mailto
: telnet
: file
: gopher
: jar
: nfs
: ldap
: jdbc
: rmi
: doc
: netdoc
: systemresource
: verbatim
環境に関するパラメータをいくつか見てみましょう.
Properties p = System.getProperties() ;
Enumeration<?> e = p.keys();
while(e.hasMoreElements()){
String str = (String) e.nextElement() ;
System.out.println( str +":"+ p.getProperty(str));
}
結果の一部:
java.runtime.name:Java(TM) SE Runtime Environment
java.vm.version:11.3-b02
java.vm.vendor:Sun Microsystems Inc.
java.vm.name:Java HotSpot(TM) Client VM
file.encoding.pkg:sun.io
sun.java.launcher:SUN_STANDARD
user.country:CN
sun.os.patch.level:Service Pack 3
java.vm.specification.name:Java Virtual Machine Specification
java.runtime.version:1.6.0_13-b03
java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment
os.arch:x86
java.vm.specification.vendor:Sun Microsystems Inc.
os.name:Windows XP
sun.jnu.encoding:GBK
java.specification.name:Java Platform API Specification
java.class.version:50.0
sun.management.compiler:HotSpot Client Compiler
os.version:5.1
java.awt.printerjob:sun.awt.windows.WPrinterJob
file.encoding:UTF-8
java.specification.version:1.6
user.name:Administrator
java.vm.specification.version:1.0
sun.arch.data.model:32
user.language:zh
java.specification.vendor:Sun Microsystems Inc.
awt.toolkit:sun.awt.windows.WToolkit
java.vm.info:mixed mode
java.version:1.6.0_13
sun.io.unicode.encoding:UnicodeLittle
sun.cpu.endian:little
sun.desktop:windows