我已经在java中准备了一个桌面应用程序。我希望通过Pendrive或任何其他USB驱动器来保护它。但我不知道如何使用java代码阅读pendrive或usb驱动器,以便我可以限制应用程序。
请帮我怎么做?或者关于这个的任何其他想法?
谢谢..:)
答案 0 :(得分:3)
就像上面提到的人一样,您可以列出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();
}
}
如果您需要,请提出任何问题!这是一个非常有趣的问题,所以如果您需要有关代码的帮助,请与我聊聊!
答案 1 :(得分:1)
答案 2 :(得分:1)
您可以使用以下代码返回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();
}
答案 3 :(得分:0)
我相信在Java SE中你只能使用File.listRoots()
method来引用Windows上的驱动器,它只返回{"C:\", "D:\", "E:\"}
这样的列表,当然,可以安装USB驱动器(基本上)任何驱动器号。
如果您想在未安装USB驱动器时限制应用程序运行,那么您可以通过在每个文件根目录下搜索它来查找驱动器上的已知文件(具有特殊内容)。
答案 4 :(得分:0)
您需要为此下载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();
}
}
}