如何使用java代码获取USB驱动器序列号或元数据

我在java中准备了一个桌面应用程序。 我希望通过Pendrive或任何其他USB驱动器来保护它。 但我不知道如何使用java代码阅读pendrive或usb驱动器,以便我可以限制应用程序。

请帮我怎么做? 或者关于这个的任何其他想法?

谢谢..:)

就像我上面说的人一样,你可以列出你的USB目录的根源。 列出根后,您可以手动找到USB驱动器,然后使用FileOutputStreams将USB驱动器中的File对象写入File,然后将该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(); } } 

如果您需要,可以提出任何问题! 这是一个非常有趣的问题,所以如果您需要有关代码的帮助,请与我聊天!

你可以试试Jinput 。 如果它不够强大,请尝试libusb 。

PS:我想补充一点,这种保护很容易破解。 那么为什么你要惩罚你的客户而不是盗版?

您可以使用以下代码返回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(); } 

我相信在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(); } } 

}