---
title: "java 通过System.getProperties()获取系统参数"
author: "foxpro"
date: "2009-09-21T14:40:46Z"
tags: []
slug: "java-system-getproperties"
draft: false
lastmod: "2009-09-21T14:40:46Z"
id: 473309
url: "https://foxpro.iteye.com/blog/473309"
---

Properties props=System.getProperties(); //系统属性
   System.out.println("Java的运行环境版本："+props.getProperty("java.version"));
   System.out.println("Java的运行环境供应商："+props.getProperty("java.vendor"));
   System.out.println("Java供应商的URL："+props.getProperty("java.vendor.url"));
   System.out.println("Java的安装路径："+props.getProperty("java.home"));
   System.out.println("Java的虚拟机规范版本："+props.getProperty("java.vm.specification.version"));
   System.out.println("Java的虚拟机规范供应商："+props.getProperty("java.vm.specification.vendor"));
   System.out.println("Java的虚拟机规范名称："+props.getProperty("java.vm.specification.name"));
   System.out.println("Java的虚拟机实现版本："+props.getProperty("java.vm.version"));
   System.out.println("Java的虚拟机实现供应商："+props.getProperty("java.vm.vendor"));
   System.out.println("Java的虚拟机实现名称："+props.getProperty("java.vm.name"));
   System.out.println("Java运行时环境规范版本："+props.getProperty("java.specification.version"));
   System.out.println("Java运行时环境规范供应商："+props.getProperty("java.specification.vender"));
   System.out.println("Java运行时环境规范名称："+props.getProperty("java.specification.name"));
   System.out.println("Java的类格式版本号："+props.getProperty("java.class.version"));
   System.out.println("Java的类路径："+props.getProperty("java.class.path"));
   System.out.println("加载库时搜索的路径列表："+props.getProperty("java.library.path"));
   System.out.println("默认的临时文件路径："+props.getProperty("java.io.tmpdir"));
   System.out.println("一个或多个扩展目录的路径："+props.getProperty("java.ext.dirs"));
   System.out.println("操作系统的名称："+props.getProperty("os.name"));
   System.out.println("操作系统的构架："+props.getProperty("os.arch"));
   System.out.println("操作系统的版本："+props.getProperty("os.version"));
   System.out.println("文件分隔符："+props.getProperty("file.separator"));   //在 unix 系统中是＂／＂
   System.out.println("路径分隔符："+props.getProperty("path.separator"));   //在 unix 系统中是＂:＂
   System.out.println("行分隔符："+props.getProperty("line.separator"));   //在 unix 系统中是＂/n＂
   System.out.println("用户的账户名称："+props.getProperty("user.name"));
   System.out.println("用户的主目录："+props.getProperty("user.home"));
   System.out.println("用户的当前工作目录："+props.getProperty("user.dir"));

 

 

2、用Java编写通过代理访问的应用程序

本技巧将向您讲述如何编写可通过代理访问因特网上的Web服务器的Java应用程序。在Java应用程序中加入代理支持只需额外编写几行代码，且不依赖任何安全性“漏洞”。

将Java和代理结合起来的秘诀即在Java运行时激活特定的系统属性。这些属性未被写入正式文件，只是作为Java传说的一部分在Java编程人员中秘传。为了支持代理，Java应用程序不仅需要指定代理本身的信息，而且需要指定用于认证的用户信息。在开始使用网际协议之前，您需要在程序中添加以下几行代码：
//通知Java您要通过代理进行连接
System.getProperties().put("proxySet","true");

//指定代理所在的服务器
System.getProperties().put("proxyHost","myProxyMachineName");

//指定代理监听的端口
System.getProperties().put("proxyPort","85"); 

　有些代理在授权用户访问因特网之前，要求用户输入用户名和口令。如果您使用位于防火墙之内的Web浏览器，您就可能碰到过这种情况。以下是执行认证的方法：

URLConnection  connection=url.openConnection();
String   password="username:password";
String   encodedPassword=base64Encode(password);
connection.setRequestProperty("Proxy-Authorization",encodedPassword); 

　　这段代码的思想是，您必须调整HTTP标头以发出用户信息。这是通过调用setRequestProperty()来实现的。这种方法允许您在发出请求之前处理HTTP标头。HTTP要求用base64对用户名和口令进行编码。幸运的是，有一组公用域API，它们将代您执行编码（请参阅参考资源部分）。

　　如您所见，在Java应用程序中加入代理支持并不需要做多少工作。有了现在的知识，再做一点研究（您必须查明您的代理是如何处理您感兴趣的协议以及如何进行用户认证的），您就能用其他协议实现代理。
    HTTP代理：（例子）

Properties props = System.getProperties();

           props.put("http.proxyHost", "192.168.0.150");

           props.put("http.proxyPort", "808");
　　FTP代理

　　ScottD.Taylor提出这个秘诀来处理FTP协议代理：

defaultProperties.put("ftpProxySet","true");
defaultProperties.put("ftpProxyHost","proxy-host-name");
defaultProperties.put("ftpProxyPort","85"); 

　　接下来您便可以通过以下代码使用"ftp"协议访问文件URL：

URL  url=newURL("ftp://ftp.netscape.com/pub/navigator/3.04/windows/readme.txt"); 

　　如果有人有使用其他网际协议代理的例子，我很想看看。

　　注意：代码示例(Example.java)仅在JDK1.1.4下测试过。

　　后续技巧！

　　对于仍在使用JDK1.1.7（配合WebSphere3.0）的开发人员而言，将proxyHost和proxyPort设为系统属性不起作用；conn.getInputStream()或者返回连接超时，或者是找不到主机路径。但是，我使用接受Host和Port为参数的URL构造函数解决了这一问题（使用我的代理主机和端口）：

public  URL(String  protocol,String  host,int port,String  file). 

　　借助用户名和口令进行认证的方法不起作用。应将"Basic"置于认证字符串的开头；例如：

String  encodedPassword=base64Encode(password); 

　　应该是：

String  encodedPassword="Basic"+base64Encode(password); 

　　您也不必用一个单独的程序来进行64位编码。您可以使用sun.misc.BASE64Encoder()类。下面是完成这两处改动之后的代码：

System.getProperties().put("proxySet","true");
System.getProperties().put("proxyHost",proxyHost);
System.getProperties().put("proxyPort",proxyPort);
String  authString="userid:password";
String  auth="Basic"+newsun.misc.BASE64Encoder().encode(authString.getBytes());
URL url=newURL("http://java.sun.com/");
URLConnection  conn=url.openConnection();
conn.setRequestProperty("Proxy-Authorization",auth); 

　　下面是使用socks4代理服务器的方法：

System.getProperty("socksProxySet",true);
System.getProperty("socksProxyHost",proxyHostName);
System.getProperty("socksProxyPort",proxyPort);
Usually  the  proxyPort  for  Socks 4  is  port  1080 



本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/foamflower/archive/2008/03/30/2229559.aspx