导航:首页 > 编程语言 > java序列号生成

java序列号生成

发布时间:2024-12-26 18:50:08

‘壹’ 跪求在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();
}
}
}
}


代码又挤在一起了:

阅读全文

与java序列号生成相关的资料

热点内容
安卓手机如何投屏到奔驰车载 浏览:722
1千块程序员礼物 浏览:498
目前外围配套最少的单片机 浏览:152
老款mac怎么进入命令行模式 浏览:810
怎么更改加密视频 浏览:257
vpn命令行 浏览:12
悬赏平台免费源码 浏览:889
游戏地图生成算法 浏览:964
java获取字段的类型 浏览:859
php开放源码 浏览:907
若水android源码 浏览:797
phpphpize安装 浏览:801
cad中点捕捉命令 浏览:31
单片机检测继电器 浏览:707
源码时代培训机构贷款 浏览:552
南光30c如何连app 浏览:821
怎么样获取对文件夹的权限 浏览:448
linuxutc时间获取 浏览:224
灵魂app是哪里的 浏览:226
云听app客服在哪里 浏览:579