我使用Jnetpcap 1.3.0版本来提取pcap文件。
下面是我的代码片段
/* Main Class */
public class Proceed{
public static void main(String[] args) {
PCapFile pcapFile = new PCapFile("C:/test/no-gre-sample.pcap");
pcapFile.process();
}
}
/in some other class I have written this method */
public void process() {
RandomAccessFile raf = null;
FileLock lock = null;
try {
raf = new RandomAccessFile(file, "rw");
lock = raf.getChannel().tryLock();
this.pcap = Pcap.openOffline(file, errbuf);
System.out.printf("Opening file for reading: %s", filePath);
if (pcap == null) {
System.err.println(errbuf); // prob occurs here
} else {
PcapPacketHandler<String> jpacketHandler;
jpacketHandler = new PcapPacketHandler<String>() {
@Override
public void nextPacket( packet, String user) {
PPacket pcap = new PPacket(packet, user);
//Process packet
}
};
// Loop over all packets in the file...
try {
pcap.loop(-1, jpacketHandler, "jNetPcap Rocks!!!!!");
} finally {
pcap.close();
}
}
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
try {
if (lock != null) {
lock.release();
}
if (raf != null) {
raf.close();
}
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
但是在eclipse(Windows)上运行时,我收到了这个错误
"读取转储文件时出错:权限被拒绝"
我也包含了.dll文件,但似乎无法理解这里的问题是什么。
注意-(此代码在Ubuntu上运行良好)
用户无法访问该文件,或者该文件被另一个进程独占打开。
// simple code to check
String filePath = "C:/test/no-gre-sample.pcap";
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(filePath, errbuf);
System.out.println("errbuf = " + errbuf);
System.out.println("pcap = " + pcap);
输出-文件不存在
errbuf = C:/test/no-gre-sample.pcap: No such file or directory
pcap = null
输出-普通用户没有访问权限
errbuf = C:/test/no-gre-sample.pcap: Permission denied
pcap = null
您可以使用cacls no-gre-sample.pcap
检查权限
C:testno-gre-sample.pcap BUILTINUsers:N
意味着组Users
(而不是特权更高的组)中的所有用户都没有此文件的权限。但是您还需要检查目录权限。
遗憾的是,Pcap.openOffline
报告的错误对于missed permissions
和read locked by another application
两种情况是相同的。您可以运行一个简单的测试来查看差异。
InputStream is = new FileInputStream(filePath);
is.read();
is.close();
错过权限的输出
Exception in thread "main" java.io.FileNotFoundException: _
C:testno-gre-sample.pcap (Access is denied)
由另一个应用程序锁定的读取输出
Exception in thread "main" java.io.FileNotFoundException: _
C:testno-gre-sample.pcap _
(The process cannot access the file because it is being used by another process)