0

我正在使用 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 上运行良好)

4

1 回答 1

0

用户无权访问此文件,或者该文件被另一个进程独占打开。

// 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:\test\no-gre-sample.pcap BUILTIN\Users:N

表示组内的所有用户Users(而不是特权更高的用户)对此文件没有权限。但是您还需要检查目录权限。

可悲的是,报告的错误Pcap.openOffline对于这两种情况missed permissionsread locked by another application. 您可以运行一个简单的测试来查看差异。

InputStream is = new FileInputStream(filePath);
is.read();
is.close();

错过许可的输出

Exception in thread "main" java.io.FileNotFoundException: _
    C:\test\no-gre-sample.pcap (Access is denied)

被另一个应用程序锁定的读取输出

 Exception in thread "main" java.io.FileNotFoundException: _
    C:\test\no-gre-sample.pcap _
    (The process cannot access the file because it is being used by another process)
于 2015-06-30T07:07:11.387 回答