java中System.getProperty()和System.setProperty()和System.getProperties()

来源:互联网 发布:最伟大的意大利人知乎 编辑:程序博客网 时间:2024/06/05 18:52

System可以有对标准输入,标准输出,错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。
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”));

java.version Java 运行时环境版本
java.vendor Java 运行时环境供应商
java.vendor.url Java 供应商的 URL
java.home Java 安装目录
java.vm.specification.version Java 虚拟机规范版本
java.vm.specification.vendor Java 虚拟机规范供应商
java.vm.specification.name Java 虚拟机规范名称
java.vm.version Java 虚拟机实现版本
java.vm.vendor Java 虚拟机实现供应商
java.vm.name Java 虚拟机实现名称
java.specification.version Java 运行时环境规范版本
java.specification.vendor Java 运行时环境规范供应商
java.specification.name Java 运行时环境规范名称
java.class.version Java 类格式版本号
java.class.path Java 类路径
java.library.path 加载库时搜索的路径列表
java.io.tmpdir 默认的临时文件路径
java.compiler 要使用的 JIT 编译器的名称
java.ext.dirs 一个或多个扩展目录的路径
os.name 操作系统的名称
os.arch 操作系统的架构
os.version 操作系统的版本
file.separator 文件分隔符(在 UNIX 系统中是“/”)
path.separator 路径分隔符(在 UNIX 系统中是“:”)
line.separator 行分隔符(在 UNIX 系统中是“/n”)
user.name 用户的账户名称
user.home 用户的主目录
user.dir 用户的当前工作目录

package com.mininglamp.util;

import java.io.File;
import java.util.Properties;

public class Test {

public static void main(String[] args) {    Properties pro= System.getProperties();    System.out.println(System.getenv());    System.out.println(pro.getProperty("user.dir"));    String currentDir = System.getProperty("user.dir");    System.out.println(currentDir);    String path = currentDir + File.separator + "conf" + File.separator + "config.properties";    System.out.println(path);    System.out.println(System.getProperty("java.version"));// 1.8.0_144    System.out.println(System.getProperty("java.vendor"));//Oracle Corporation    System.out.println(System.getProperty("java.vendor.url"));//http://java.oracle.com/    System.out.println(System.getProperty("java.home"));//D:\java\jre    System.out.println(System.getProperty("java.vm.specification.version"));//1.8    System.out.println(System.getProperty("java.vm.specification.vendor"));//Oracle Corporation    System.out.println(System.getProperty("java.vm.specification.name"));//Java Virtual Machine Specification    System.out.println(System.getProperty("java.vm.version"));//25.144-b01    System.out.println(System.getProperty("java.vm.vendor"));//Oracle Corporation    System.out.println(System.getProperty("java.vm.name"));//Java HotSpot(TM) 64-Bit Server VM    System.out.println(System.getProperty("java.specification.version"));//1.8    System.out.println(System.getProperty("java.specification.vendor"));//Oracle Corporation    System.out.println(System.getProperty("java.specification.name"));//Java Platform API Specification    System.out.println(System.getProperty("java.class.version"));//52.0    System.out.println(System.getProperty("java.class.path"));//    System.out.println(System.getProperty("java.library.path"));////  D:\java\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/java/bin/../jre/bin/server;D:/java/bin/../jre/bin;D:/java/bin/../jre/lib/amd64;D:\java\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program File    System.out.println(System.getProperty("java.io.tmdir"));//null    System.out.println(System.getProperty("java.compiler"));//null    System.out.println(System.getProperty("java.ext.dirs"));//D:\java\jre\lib\ext;C:\Windows\Sun\Java\lib\ext    System.out.println(System.getProperty("os.name"));//Windows 7    System.out.println(System.getProperty("os.arch"));//amd64    System.out.println(System.getProperty("os.version"));//6.1    System.out.println(System.getProperty("file.separator"));// \(windows) /(unix)    System.out.println(System.getProperty("path.separator"));// ;(windows) :(unix)    System.out.println(System.getProperty("line.separator"));//在unix系统中是“/n”, window(空行)    System.out.println(System.getProperty("user.name"));//syf    System.out.println(System.getProperty("user.home"));//C:\Users\syf    System.out.println(System.getProperty("user.dir"));//D:\WorkSpaces\SocialGnDomestic    System.out.println(System.getProperty("file.encoding"));//UTF-8}

}

3.
用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

阅读全文
0 0