J2SE1.5也被称为J2SE5.0,在5.0以前的代理服务器设置挺不友好,现在,5.0提供了对通过代理服务器的更加灵活的控制,它可以对http,https,ftp,socks等分别设置,而且还可以设置不需要通过代理服务器的主机和地址。这和我们在IE、firefox中设置代理服务器类似。
1.你可以在通过Java -DXXXX=YYYY方式在程序启动时设置,你也可以在程序中将设置放入系统属性中,你也可以设置Proxy类,通过它来控制。
2.可以通过ProxySelector来做自己的代理服务器的负载平衡等。
package com.kuaff.JDK5package;import java.io.IOException;import java.io.InputStream;import java.net.InetSocketAddress;import java.net.MalformedURLException;import java.net.Proxy;import java.net.ProxySelector;import java.net.SocketAddress;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Properties;public class NetProxy{// 测试本地JVM的网络缺省配置public void setLocalProxy(){Properties prop = System.getProperties();//设置http访问要使用的代理服务器的地址prop.setProperty("http.proxyHost", "10.10.0.96");//设置http访问要使用的代理服务器的端口prop.setProperty("http.proxyPort", "8080");//设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔prop.setProperty("http.nonProxyHosts", "localhost|10.10.*");//设置安全访问使用的代理服务器地址与端口//它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问prop.setProperty("https.proxyHost", "10.10.0.96");prop.setProperty("https.proxyPort", "443");//使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机prop.setProperty("ftp.proxyHost", "10.10.0.96");prop.setProperty("ftp.proxyPort", "2121");prop.setProperty("ftp.nonProxyHosts", "localhost|10.10.*");//socks代理服务器的地址与端口prop.setProperty("socksProxyHost", "10.10.0.96");prop.setProperty("socksProxyPort", "1080");}// 清除proxy设置public void removeLocalProxy(){Properties prop = System.getProperties();prop.remove("http.proxyHost");prop.remove("http.proxyPort");prop.remove("http.nonProxyHosts");prop.remove("https.proxyHost");prop.remove("https.proxyPort");prop.remove("ftp.proxyHost");prop.remove("ftp.proxyPort");prop.remove("ftp.nonProxyHosts");prop.remove("socksProxyHost");prop.remove("socksProxyPort");}//// 测试httppublic void showHttpProxy(Object... proxy){URL url = null;try{url = new URL("http://blog.csdn.com/smallnest");}catch (MalformedURLException e){return;}try{URLConnection conn = null;switch (proxy.length){case 0:conn = url.openConnection();break;case 1:conn = url.openConnection((Proxy) proxy[0]);break;default:break;}if (conn == null)return;conn.setConnectTimeout(3000); // 设置连接超时时间InputStream in = conn.getInputStream();byte[] b = new byte[1024];try{while (in.read(b) > 0){System.out.println(new String(b));}}catch (IOException e1){}}catch (IOException e1){e1.printStackTrace();}}// 测试ftppublic void showFtpProxy(Object... proxy){URL url = null;try{url = new URL("ftp://ftp.tsinghua.edu.cn");}catch (MalformedURLException e){return;}try{URLConnection conn = null;switch (proxy.length){case 0:conn = url.openConnection();break;case 1:conn = url.openConnection((Proxy) proxy[0]);break;default:break;}if (conn == null)return;conn.setConnectTimeout(3000); // 设置连接超时时间InputStream in = conn.getInputStream();byte[] b = new byte[1024];try{while (in.read(b) > 0){System.out.println(new String(b));}}catch (IOException e1){}}catch (IOException e1){e1.printStackTrace();}}// 得到一个proxypublic Proxy getProxy(Proxy.Type type, String host, int port){SocketAddress addr = new InetSocketAddress(host,port)

roxy typeProxy = new Proxy(type, addr);return typeProxy;}public static void main(String[] args){NetProxy proxy = new NetProxy();//测试代理服务器proxy.setLocalProxy();proxy.showHttpProxy();//下面两行是清除系统属性,而通过Proxy类指定代理服务器// proxy.removeLocalProxy//proxy.showHttpProxy(proxy.getProxy(Proxy.Type.SOCKS,"10.10.0.96",1080));}}