简体   繁体   English

使用Java发现USB海量存储设备

[英]Discovering USB Mass Storage Devices with Java

The story behind... 背后的故事......

I really like TV shows but I go back home only twice a month. 我非常喜欢电视节目,但我每个月只回家两次。 The rest of the time I live in a house without internet (close to my university though, so free wifi rocks! - when it works - ) so I needed a little software that was able to update my portable hard-disk with my new shows when I go back home where the file server synchronized with podcasts does its job. 其余的时间我住在没有互联网的房子里(虽然靠近我的大学,所以免费无线网络摇滚! - 当它工作时 - )所以我需要一个小软件能够用我的新节目更新我的便携式硬盘当我回到家里,文件服务器与播客同步完成它的工作。 I did it using Java and it works. 我是用Java做的,它可以工作。

The problem 问题

Right now I have a .properties file where I stored the mounted directory of that usb hd. 现在我有一个.properties文件,我存储了那个usb hd的挂载目录。 Not enough. 不够。 I want the software to be able to discover all of USB mass storage devices and let the user select which one use to store files. 我希望软件能够发现所有USB大容量存储设备,并让用户选择用于存储文件的设备。 How can I do that? 我怎样才能做到这一点?

Details 细节

a) it has to be in Java (I mean, It could also work with executing local host commands like dir or something like that) a)它必须是Java(我的意思是,它也可以用于执行本地主机命令,如dir或类似的东西)

b) my server is on windows, but I prefer it to be an OS independent solution b)我的服务器在Windows上,但我更喜欢它是一个独立于操作系统的解决方案

While I didn't see it in your question, I presume you are very familiar with File.listRoots() method which returns an array of well, file roots. 虽然我没有在你的问题中看到它,但我认为你非常熟悉File.listRoots()方法,该方法返回一个井,文件根数组。

Then, you could just iterate over them, and try to identify if they are flash drives. 然后,您可以迭代它们,并尝试确定它们是否是闪存驱动器。 Some hacks may be like: 一些黑客可能是这样的:

File[] roots = File.listRoots();

if (roots == null) {
  // you have a different problem here.  Is it even a computer we are talking about?
}

// Iterate through roots
for (File root : roots) {
    if (root.canWrite()) {  // Or, you could use File.createTempfile with that folder
        // if root does not contain some well know files
        // Or, if root contains some well known files
        // Based on these 2 hacks, or possible other characteristics, you may be reasonably sure
    }
}

That's all I can offer. 这就是我所能提供的一切。 A lot more can be done with more native programs, and then invoking them from the Java program. 使用更多本机程序可以完成更多工作,然后从Java程序中调用它们。

A beautiful way has been provided in nio. nio提供了一种美丽的方式。 Check this out: 看一下这个:

import java.nio.file.*;
for(Path p:FileSystems.getDefault().getRootDirectories()){
    System.out.println(p);
}

You'll get All roots as path objects; 你会得到所有的根作为路径对象;

You can check the javax.usb project. 您可以查看javax.usb项目。 Currently there are two certified implementations, for Linux and BSD, and a implementation for windows (as it seems, its still not complete, but I supose it allows you to list the USB devices conected). 目前有两种经过认证的实现,用于Linux和BSD,以及一种用于Windows实现 (看起来,它仍然不完整,但我想它可以列出连接的USB设备)。

But I'm not sure if the posibility of listing only USB drives (instead all drives like in @Amrinder Arora answer) worth adopt a new library set and struggle with a semi-complete implementation... 但是我不确定只列出USB驱动器的可能性(而不是像@Amrinder Arora那样的所有驱动器的答案)值得采用一个新的库设置并且与半完整的实现斗争...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM