Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 通过使用SpringBoot插入来自循环的所有数据来创建JSON数组_Arrays_Json_Spring Boot - Fatal编程技术网

Arrays 通过使用SpringBoot插入来自循环的所有数据来创建JSON数组

Arrays 通过使用SpringBoot插入来自循环的所有数据来创建JSON数组,arrays,json,spring-boot,Arrays,Json,Spring Boot,我已经实现了一个从.pcap文件导入数据的代码。它工作正常。它读取cap文件,并在控制台中显示结果 代码是 @SpringBootApplication public class SpringBootSecurityJwtMongodbApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSecurityJwtMongodbApplication.c

我已经实现了一个从.pcap文件导入数据的代码。它工作正常。它读取cap文件,并在控制台中显示结果

代码是

@SpringBootApplication

public class SpringBootSecurityJwtMongodbApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootSecurityJwtMongodbApplication.class, args);
    }


    @Bean
    public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) {
        return args -> {
            List<File> files = gateFile.mget(".");
            for (File file : files) {
                System.out.println("Result:" + file.getAbsolutePath());
                run(file);
            }
        };
    }

    void run(File file) throws IOException {
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
        Pcap pcap = Pcap.openStream(file);
        pcap.loop(
                packet -> {
                    if (packet.hasProtocol(Protocol.TCP)) {
                        TCPPacket packet1 = (TCPPacket) packet.getPacket(Protocol.TCP);
                        String Time = formatter.format(new Date(packet1.getArrivalTime() / 1000));
                        String Source = packet1.getSourceIP();
                        String Destination = packet1.getDestinationIP();
                        String Protocol = packet1.getProtocol().toString();
                        Long Length = packet1.getTotalLength();
                        System.out.printf("%s | %s | %s | %s | %d \n", Time, Source, Destination, Protocol, Length);
                       } else if (packet.hasProtocol(Protocol.UDP)) {
                        UDPPacket packet1 = (UDPPacket) packet.getPacket(Protocol.UDP);
                        String Time = formatter.format(new Date(packet1.getArrivalTime() / 1000));
                        String Source = packet1.getSourceIP();
                        String Destination = packet1.getDestinationIP();
                        String Protocol = packet1.getProtocol().toString();
                        Long Length = packet1.getTotalLength();
                        System.out.printf("%s | %s | %s | %s | %d \n", Time, Source, Destination, Protocol, Length);
                    } else {
                        System.out.println("Not found protocol. | " + packet.getProtocol());
                    }
                   return packet.getNextPacket() != null;
                }
        );
    }
}

有人能帮我做这件事吗?

只需创建
JSONArray
的一个实例,并将其传递给
run
方法,然后在
packet->{}
方法体中创建
JSONObject
的一个实例,然后填充
JSONObject
并将其推送到
JSONArray
的传递实例。以下是最低限度的代码:

@Bean
public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) {
    ...
    JSONArray arr = new JSONArray();
    for (File file : files) {
        System.out.println("Result:" + file.getAbsolutePath());
        run(file, arr);
    }
    ...
}

void run(File file, JSONArray arr) throws IOException {
    ...
        packet -> {
            ...
            if (packet.hasProtocol(Protocol.TCP)) {
                ...
                JSONOBject obj = new JSONOBject();
                obj.put("Desitnation", destination);
                // set other properties
                arr.add(obj);
                ...
            }
            // same for else if block
            ...
        }
    ...
}
@Bean
public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) {
    ...
    JSONArray arr = new JSONArray();
    for (File file : files) {
        System.out.println("Result:" + file.getAbsolutePath());
        run(file, arr);
    }
    ...
}

void run(File file, JSONArray arr) throws IOException {
    ...
        packet -> {
            ...
            if (packet.hasProtocol(Protocol.TCP)) {
                ...
                JSONOBject obj = new JSONOBject();
                obj.put("Desitnation", destination);
                // set other properties
                arr.add(obj);
                ...
            }
            // same for else if block
            ...
        }
    ...
}