我已经用java准备了一个桌面应用程序。我想通过Pendrive或任何其他usb驱动器来保护它。但我不知道如何使用java代码读取pendrive或usb驱动器,这样我就可以限制应用程序。
请帮我怎么做?或者对此有其他想法吗?
谢谢..:)
就像我上面的人说的,你可以列出你的USB目录的根。列出根目录后,您可以手动找到USB驱动器,然后使用FileOutputStreams将File对象从USB驱动器写入文件,然后稍后将该File对象与驱动器进行比较。或者你可以在你的USB驱动器中创建一个唯一的文件名并使用
File[] roots = File.listRoots();for(int i = 0; i < roots.length; i++){
File[] filesInRoot = File.listRoots()[i].listFiles();
for(int j = 0; j < filesInRoot.length; j++){
if(filesInRoot[j].getName().equals(yourUniqueFileName))
executeYourCode();
}
}
如果你需要,可以问任何问题!这是一个非常有趣的问题,所以如果你需要代码方面的帮助,请与我聊天!
您可以使用以下代码返回USB或磁盘驱动器。
public String HDD_SerialNo(){
StringBuffer HDD_Serial=new StringBuffer();
String HDD_Number;
Runtime rt = Runtime.getRuntime();
try {
Process process=rt.exec(new String[]{"CMD", "/C", "WMIC diskdrive get serialnumber"});
String s = null;
//Reading sucessful output of the command
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((s = reader.readLine()) != null) {
HDD_Serial.append(s);
}
// Reading error if any
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((s = reader.readLine()) != null) {
System.out.print(s);
}
} catch (IOException e) {
e.printStackTrace();
}
您可以尝试Jinput。如果它对您来说不够强大,请尝试libusb。
附言:我想补充一点,这种保护很容易被破解。那么,你为什么要惩罚你的客户不盗版呢?
我相信在Java SE中,您只能通过使用File.listRoots()
方法来引用Windows上的驱动器,该方法只会返回类似{"C:", "D:", "E:"}
的列表,当然,USB驱动器可以安装在(基本上)任何驱动器号上。
如果您想限制应用程序在未安装USB驱动器时运行,那么您可以通过在每个文件根下搜索驱动器上的已知文件(具有特殊内容)来查找该文件。
您需要下载jusb-api。
package usb.main;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import usb.core.Bus;
import usb.core.Configuration;
import usb.core.Endpoint;
import usb.core.Host;
import usb.core.HostFactory;
import usb.core.Interface;
import usb.windows.DeviceImpl;
public class Test {
public static void main(String[] args) {
try {
Host host = HostFactory.getHost();
Bus[] bus = host.getBusses();
SearchUSBDevices(bus);
} catch (Exception e) {
}
}
private static void SearchUSBDevices(Bus[] bus) {
boolean FOUND_FLAG = false;
try {
DeviceImpl dev;
for (int k = 0; k < bus.length; k++) {
for (int i = 0; i < 5; i++) {
dev = (DeviceImpl) bus[k].getDevice(i);
if (dev != null) {
// System.out.println(dev.getString(1, 1));
if (dev.getAddress() >= 1) {
if (dev.getNumPorts() > 0)
;
else {
// System.out.println("Device Name :"+dev.getFriendlyDeviceName());
if (dev.getFriendlyDeviceName()
.equalsIgnoreCase(
"USB Mass Storage Device")) {
FOUND_FLAG = true;
// // START
//
if (dev != null) {
// Obtain the current Configuration of
// the device and the number of
// Interfaces available under the
// current Configuration.
Configuration config = dev
.getConfiguration();
int total_interface = config
.getNumInterfaces();
// Traverse through the Interfaces
for (int k1 = 0; k1 < total_interface; k1++) {
// Access the currently Interface
// and obtain the number of
// endpoints available on the
// Interface.
Interface itf = config
.getInterface(k1, 0);
int total_ep = itf
.getNumEndpoints();
System.out.println(total_ep);
// Traverse through all the
// endpoints.
for (int l = 0; l < total_ep - 1; l++) {
// Access the endpoint, and
// obtain its I/O type.
Endpoint ep = itf
.getEndpoint(l);
String io_type = ep.getType();
System.out.println(io_type);
boolean input = ep.isInput();
System.out.println(input);
// If the endpoint is an input
// endpoint, obtain its
// InputStream and read in data.
if (input) {
InputStream in;
in = ep.getInputStream();
// Read in data here
/*
* byte[] b = new byte[100];
* in.read(b);
*/
BufferedReader bReader = new BufferedReader(
new InputStreamReader(
in));
String line;
/*
* while ((line =
* bReader.read
* (cbuf))!=null){
* System.out
* .println(line); }
*/
in.close();
}
// If the Endpoint is and output
// Endpoint, obtain its
// OutputStream and write out
// data.
else {
OutputStream out;
out = ep.getOutputStream();
// Write out data here.
out.close();
}
}
}
}
// END
}
}
}
}
}
}
if (FOUND_FLAG)
System.out.println("Pendrive Found..");
else
System.out.println("Pendrive Not Found ...");
} catch (Exception e) {
e.printStackTrace();
}
}
}