1.
System.getProperties()可以确定当前的系统属性,返回值是一个Properties;
System.load(String filename)等同于:System.getProperties().load(String filename)它们的作用是可以从作为动态库德本地文件系统中指定的文件名加载代码文件。
System.setProperties(Properties propes):将系统属性设置为Properties参数;
System.setProperties(String key,String value)等同于System.getProperties().setProperties(String key,String value):设置指定键指示的系统属性
对于在程序中如果我们想得到一个资源文件中对应的键值对的内容,可以有两种方法:
1)使用Properties的load方法,将这个文件先加载进来,之后使用getProperty方法将对应键的值得到,比如:
System.getProperties().load("System.Properties.txt");先加载System.Properties.txt文件
System.getProperties().getProperty("DBType");后将文件中键为DBType的值得到。
2)使用第一种方法键对应的值得灵活性比较大。还有一种方法是将不从文件中得到键对应的值。在程序中去设一个属性,比如:
System.getProperties().setProperty("DBType","SQLServer");先设置一个键位DBType的属性
System.getProperties().getProperty("DBType");后通过getProperty方法得到DBType的值。
另外使用Properties.getProperty方法的参数也可以使用系统的一些环境变量,列表如下:
Key Meaning
------------------- ------------------------------
"file.separator" File separator (e.g., "/")
"java.class.path" Java classpath
"java.class.version" Java class version number
"java.home" Java installation directory
"java.vendor" Java vendor-specific string
"java.vendor.url" Java vendor URL
"java.version" Java version number
"line.separator" Line separator
"os.arch" Operating system architecture
"os.name" Operating system name
"path.separator" Path separator (e.g., ":")
"user.dir" User's current working directory
"user.home" User home directory
"user.name" User account name
使用其中的key可以得到一些属性,供我们在程序中使用
备注:
Microsoft VM是WIN32操作环境中的虚拟机,VM一般安装在大多数操作系统下,也包含在多数IE中。
Microsoft VM存在漏洞允许攻击者对user.dir属性进行访问。user.dir属性包含当前应用程序的工作目录信息,也包含用户名信息,利用这个漏洞可以获得当前用户名称。
可以利用WEB页和HTML形式邮件来触发。
Java获取当前系统的编码方式:
Properties initProp = new Properties(System.getProperties()); System.out.println("file.encoding:"+initProp.getProperty("file.encoding")); System.out.println("file.encoding:"+initProp.getProperty("user.language"));
2、用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");