获取PC硬件信息方法一:利用Java Applet



Java Applet可以运行在网页上面,实现功能是通过调用Java来获取本地硬盘信息,前提是本机必须安装Java环境才能运行,不然会提示你安装。

下面是代码:

package com.test;

import java.applet.Applet;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class HardWareUtils extends Applet{

	public HardWareUtils(){
	}
	private static final long serialVersionUID = 1L;

	@Override
	public void paint(Graphics paint) {
		super.paint(paint);
		paint.drawString("获取硬件信息", 10, 10);
		paint.drawString("CPU  SN:" + HardWareUtils.getCPUSerial(), 10, 30);
		paint.drawString("主板  SN:" + HardWareUtils.getMotherboardSN(), 10, 50);
		paint.drawString("C盘   SN:" + HardWareUtils.getHardDiskSN("c"), 10, 70);
		paint.drawString("MAC  SN:" + HardWareUtils.getMac(), 10, 90);
	}

	/**
	 * 获取主板序列号
	 * 
	 * @return
	 */
	public static String getMotherboardSN() {
		String result = "";
		try {
			File file = File.createTempFile("realhowto", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);
			String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
					+ "Set colItems = objWMIService.ExecQuery _ \n"
					+ "   (\"Select * from Win32_BaseBoard\") \n"
					+ "For Each objItem in colItems \n"
					+ "    Wscript.Echo objItem.SerialNumber \n"
					+ "    exit for  ' do the first cpu only! \n" + "Next \n";

			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.trim();
	}

	/**
	 * 获取硬盘序列号
	 * 
	 * @param drive
	 *            盘符
	 * @return
	 */
	public static String getHardDiskSN(String drive) {
		String result = "";
		try {
			File file = File.createTempFile("realhowto", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
					+ "Set colDrives = objFSO.Drives\n"
					+ "Set objDrive = colDrives.item(\""
					+ drive
					+ "\")\n"
					+ "Wscript.Echo objDrive.SerialNumber"; // see note
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.trim();
	}

	/**
	 * 获取CPU序列号
	 * 
	 * @return
	 */
	public static String getCPUSerial() {
		String result = "";
		try {
			File file = File.createTempFile("tmp", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);
			String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
					+ "Set colItems = objWMIService.ExecQuery _ \n"
					+ "   (\"Select * from Win32_Processor\") \n"
					+ "For Each objItem in colItems \n"
					+ "    Wscript.Echo objItem.ProcessorId \n"
					+ "    exit for  ' do the first cpu only! \n" + "Next \n";
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
			file.delete();
		} catch (Exception e) {
			e.fillInStackTrace();
		}
		if (result.trim().length() < 1 || result == null) {
			result = "无CPU_ID被读取";
		}
		return result.trim();
	}

	/**
	 * 获取MAC地址
	 */
	public static String getMac() {
		try {
			byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < mac.length; i++) {
				if (i != 0) {
					sb.append("-");
				}
				String s = Integer.toHexString(mac[i] & 0xFF);
				sb.append(s.length() == 1 ? 0 + s : s);
			}
			return sb.toString().toUpperCase();
		} catch (Exception e) {
			return "";
		}

	}
}

运行效果如下:

获取PC硬件信息方法一:利用Java Applet_第1张图片

注意:如果直接在Eclipse里面运行正常,如果嵌入到网页中想要正常获取必须进行签名。


对Java Applet进行签名的方法如下:

怎样对java小程序签名受到很多人的关注,网上也有这些方面可供参考的文档,但都不是完整,在这里我给出一份可具体操作的文档,希望能给大家一些帮助。 场景:test.jar:内含待签名的java小程序 结果:签名后,客户端能自动下载java小程序,且java小程序能访问客户端的任何资源

步骤:

(1)将java小程序打包 
  jar cvf test.jar test.class

(2)生成名为chinani.store的密钥库,别名为chinani 
  keytool -genkey -keystore chinani.store -alias chinani
密码:123456 (根据需要自己输入) 以下根据需要输入,最后确认:y

(3)导出chinani.cer数字证书 
  keytool -export -keystore chinani.store -alias chinani -file chinani.cer
密码:123456 (根据需要自己输入)

(4)对test.jar文件进行签名 
  jarsigner -keystore chinani.store  test.jar chinani
密码:123456 (根据需要自己输入)

  最后写一个html文件,运行此文件即可自动下载安装运行java小程序HTML文件内容如下:

 
archive="Test.jar" 
width=320 height=300> 


你可能感兴趣的:(java,基础)