『壹』 跪求在java里如何獲得CPU的序列號,和硬碟的序列號。
利用Runtime call操作系統的命令,具體的命令取決於不同的操作系統,注意不要調用Runtime.getRuntime().exec(String)介面,要用Runtime.getRuntime().exec(String[])這個介面,不然復雜命令的執行會有問題。例子如下(拿cpu個數,其他類似):
定義命令:
WindowsCmd ="cmd.exe /c echo %NUMBER_OF_PROCESSORS%";//windows的特殊
SolarisCmd = {"/bin/sh", "-c", "/usr/sbin/psrinfo | wc -l"};
AIXCmd = {"/bin/sh", "-c", "/usr/sbin/lsdev -Cc processor | wc -l"};
HPUXCmd = {"/bin/sh", "-c", "echo \"map\" | /usr/sbin/cstm | grep CPU | wc -l "};
linuxCmd = {"/bin/sh", "-c", "cat /proc/cpuinfo | grep ^process | wc -l"};
然後判斷系統:
os = System.getProperty("os.name").toLowerCase();
根據不同的操作系統call不同的命令。
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class GetMACAddress
{
public String getMACAddress(String ipAddress)
{
String str = "",strMAC = "",macAddress = "";
try
{
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for(int i = 1;i < 100;i++)
{
str = input.readLine();
if(str != null)
{
if(str.indexOf("MAC Address") > 1)
{
strMAC = str.substring(str.indexOf("MAC Address") + 14,str.length());
break;
}
}
}
}
catch(IOException ex)
{
return "Can't Get MAC Address!";
}
//
if(strMAC.length() < 17)
{
return "Error!";
}
macAddress = strMAC.substring(0,2) + ":"
+ strMAC.substring(3,5) + ":"
+ strMAC.substring(6,8) + ":"
+ strMAC.substring(9,11) + ":"
+ strMAC.substring(12,14) + ":"
+ strMAC.substring(15,17);
//
return macAddress;
}
public static void main(String[] args)
{
GetMACAddress getMACAddress = new GetMACAddress();
System.out.println(getMACAddress.getMACAddress("172.18.8.225"));
try
{
java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");
InputStream istr = proc.getInputStream();
byte[] data = new byte[1024];
istr.read(data);
String netdata = new String(data);
System.out.println("Your Mac Address=" + procAll(netdata));
}
catch(IOException e)
{
System.out.println("error=" + e);
}
}
public static String procAll(String str)
{
return procStringEnd(procFirstMac(procAddress(str)));
}
public static String procAddress(String str)
{
int indexof = str.indexOf("Physical Address");
if(indexof > 0)
{
return str.substring(indexof,str.length());
}
return str;
}
public static String procFirstMac(String str)
{
int indexof = str.indexOf(":");
if(indexof > 0)
{
return str.substring(indexof + 1,str.length()).trim();
}
return str;
}
public static String procStringEnd(String str)
{
int indexof = str.indexOf("\r");
if(indexof > 0)
{
return str.substring(0,indexof).trim();
}
return str;
}
}
import java.util.Vector;
class GetNetMAC
{
//網卡物理地址長度
static private final int _physicalLength = 16;
public static void main(String[] args)
{
//output you computer phycail ip address
System.out.println("The MAC Addressis:\t" + getPhysicalAddress());
}
static public String getPhysicalAddress()
{
GetNetMACShell shell = new GetNetMACShell();
String cmd = "cmd.exe /c ipconfig/all";
Vector result;
result = shell.execute(cmd);
return parseCmd(result.toString());
}
//從字元串中解析出所需要獲得的字元串
static private String parseCmd(String s)
{
String find = "Physical Address. . . . . . . . . :";
int findIndex = s.indexOf(find);
if(findIndex == -1)
{
return "not find";
}
else
{
return s.substring(findIndex + find.length() + 1,findIndex + find.length() + 1 + _physicalLength);
}
}
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
public class GetNetMACShell
{
private Process process = null;
public Vector execute(String shellCommand)
{
try
{
Start(shellCommand);
Vector vResult = new Vector();
DataInputStream in = new DataInputStream(process.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
do
{
line = reader.readLine();
if(line == null)
{
break;
}
else
{
vResult.addElement(line);
}
}
while(true);
reader.close();
return vResult;
}
catch(Exception e)
{
//error
return null;
}
}
public void Start(String shellCommand)
{
try
{
if(process != null)
{
kill();
}
Runtime sys = Runtime.getRuntime();
process = sys.exec(shellCommand);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void kill()
{
if(process != null)
{
process.destroy();
process = null;
}
}
}
試試是否可以:)
『貳』 java如何獲取本機主板序列號
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();
}
System.out.println(result);
return result.trim();
}
public static void main(String[] args)
{
getMotherboardSN();
}
這個是我在網上找的,但是只能在windows下獲得主板序列號,在linux下就不行。我愁~在linux下如何獲得主板序列號呢。
『叄』 用java實現字母與數字混合的唯一序號,且要遞增
importjava.util.LinkedList;
publicclassReplaceNumber
{
publicstaticvoidmain(String[]args)
{
LinkedList<String>result=newLinkedList<String>();
charr='0',g='0',b='0';
Stringrgb=""+r+g+b;
while(!"ZZZ".equals(rgb))
{
if(b<':'||(b>='A'-1&&b<='Z'))
{
b++;
}
if(':'==b||'Z'+1==b)
{
if(g<':')
{
g++;
b='9';
}
if(g<'Z'+1&&g>':')
{
g++;
b='Z';
}
}
if(':'==g||'Z'+1==g)
{
if(r<'9')
{
r++;
g='9';
}
if(r<'Z'+1&&r>':')
{
r++;
g='Z';
}
}
rgb=""+r+g+b;
if("999".equals(rgb))
{
r='A';
g='0';
b='0';
}
if("A99".equals(rgb))
{
r='A';
g='A';
b='0';
}
if("AA9".equals(rgb))
{
r='A';
g='A';
b='A'-1;
}
result.add(rgb);
}
System.out.println("唯一序列號: "+result.toString().replaceAll("[\[\]]","").replaceAll("\,"," "));
}
}
『肆』 求java自動生成一個序列號的方法,急急急...
package com.test4;
public class Test7 {
public static void main(String[] args) {
System.out.println(getNum("20100505",3));
}
//假設資料庫里有個20100505005的編號
private static String getNum(String firstPart, int len) {
//調用資料庫獲得20100505005這個編號
String oldNum = "20100505005";
int num = Integer.parseInt(oldNum.replace(firstPart,""));
String numStr = ++num +"";
int length = numStr.length();
for (int i = length; i < len; i++) {
numStr = "0"+numStr;
}
return firstPart + numStr;
}
}
『伍』 如何用JAVA生成注冊序列號
平常我們都接觸過軟體注冊,輸入序列號、激活碼、注冊碼、授權碼;對於這些字元碼到底代表什麼含義不甚了解,但一般來說,這些字元碼中都有幾個特點:
1、唯一性,肯定是一個唯一的序列號,否則就會存在濫用的問題。
2、加密性,肯定是經過加密或者混亂的,防止大家自己生成序列號。
3、解密性,軟體自身肯定可以解密,否則無法驗證合法性。
4、可讀性,序列號一般都比較標准,方便書寫和記憶,所以一般都為數字和字母。
以下給出簡單示例:
[java] view plain
/**
* byte轉哈希
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs += ("0" + stmp);
else
hs += stmp;
}
return hs.toUpperCase();
}
/**
* 哈希轉byte
* @param b
* @return
*/
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("長度不是偶數");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
『陸』 Java如何生成版本號比如0001 0002 0002
首先還是不太明白你說的這個「生成版本號」是什麼意思,如果只是生成一個自增序列的話
1、如果有oracle資料庫的話可以利用它的序列生成。
2、沒有oracle,用redis也行。
3、沒有資料庫,那就寫個文件來存取吧:
public class Test2 {
public static void main(String[] args) throws IOException {
System.out.println(getSequence("d:\test\sequence.txt"));
setSequence("d:\test\sequence.txt", "");
System.out.println(getSequence("d:\test\sequence.txt"));
}
//讀取序列
public static String getSequence(String sequenceFile) throws IOException {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
File file = new File(sequenceFile);
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream);
bufferedReader = new BufferedReader(inputStreamReader);
// 按行讀取字元串
String str;
if ((str = bufferedReader.readLine()) != null) {
return str;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
//設置序列,如果傳入的序列號為空,則在原序列的基礎上+1
public static void setSequence(String sequenceFile, String sequence) throws IOException {
if (sequence == null || sequence.isEmpty()) {
String oriSequence = getSequence(sequenceFile);
Objects.requireNonNull(oriSequence);
sequence = String.format("%04d", Integer.valueOf(oriSequence) + 1);
}
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try {
File file = new File(sequenceFile);
fileOutputStream = new FileOutputStream(file);
outputStreamWriter = new OutputStreamWriter(fileOutputStream);
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(sequence);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
bufferedWriter.close();
}
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
}
代碼又擠在一起了: