用java1.8的maven的springboot项目在20.12和20.16两台虚拟机分别启动服务同时监听多个目录下的不同文件,如果监听到了一个目录下的文件,就把文件复制到/data/file里然后结束这个目录文件的监听,继续监听其他目录文件,通过接口调用开启监听同时返回监听状态,通过接口控制停止监听同时返回监听状态
为了完成你的需求,我们需要完成以下任务:
- 创建一个Spring Boot项目,使用Maven进行构建。
- 实现目录和文件的监听功能。
- 实现文件复制功能。
- 提供RESTful接口来启动和停止监听,同时返回监听状态。
以下是详细的实现步骤:
第一步:创建Spring Boot项目
首先,使用Spring Initializr生成一个Spring Boot项目。选择以下依赖:
- Spring Web
- Spring Boot DevTools
生成项目后,下载并解压。
第二步:添加依赖
在pom.xml
中添加以下依赖来支持文件监听:
<dependencies>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- Directory and file watching dependencies -->
<dependency>
<groupId>com.github.drapostolos</groupId>
<artifactId>rwscf</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
第三步:实现文件监听服务
在Spring Boot项目中,创建一个服务类FileWatcherService
来监听目录和文件的变化。
package com.example.filewatcher.service;
import com.github.drapostolos.rwscf.FileSystemChangeObserver;
import com.github.drapostolos.rwscf.FileSystemChangeWatcher;
import com.github.drapostolos.rwscf.OnFileChange;
import com.github.drapostolos.rwscf.predicate.Filters;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
@Service
public class FileWatcherService {
private final Map<String, FileSystemChangeWatcher> watchers = new HashMap<>();
@PostConstruct
public void init() {
// Initialization code if needed
}
public void startWatching(String dirPath) throws IOException {
Path path = Paths.get(dirPath);
FileSystemChangeWatcher watcher = FileSystemChangeWatcher.newBuilder()
.addObserver(new FileSystemChangeObserver() {
@Override
@OnFileChange
public void onChange(Path changedFilePath) {
try {
// Handle file change
Files.copy(changedFilePath, Paths.get("/data/file/" + changedFilePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
stopWatching(dirPath);
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addListenerFilters(Filters.every(5, TimeUnit.SECONDS))
.path(path)
.build();
watcher.start();
watchers.put(dirPath, watcher);
}
public void stopWatching(String dirPath) {
FileSystemChangeWatcher watcher = watchers.remove(dirPath);
if (watcher != null) {
watcher.stop();
}
}
public Map<String, String> getStatus() {
Map<String, String> status = new HashMap<>();
watchers.forEach((dirPath, watcher) -> {
status.put(dirPath, watcher.isRunning() ? "Running" : "Stopped");
});
return status;
}
}
第四步:创建RESTful接口
在控制器类FileWatcherController
中定义RESTful接口来启动和停止文件监听,同时返回监听状态。
package com.example.filewatcher.controller;
import com.example.filewatcher.service.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;
@RestController
@RequestMapping("/watcher")
public class FileWatcherController {
@Autowired
private FileWatcherService fileWatcherService;
@PostMapping("/start")
public Map<String, String> startWatching(@RequestParam String dirPath) throws IOException {
fileWatcherService.startWatching(dirPath);
return fileWatcherService.getStatus();
}
@PostMapping("/stop")
public Map<String, String> stopWatching(@RequestParam String dirPath) {
fileWatcherService.stopWatching(dirPath);
return fileWatcherService.getStatus();
}
@GetMapping("/status")
public Map<String, String> getStatus() {
return fileWatcherService.getStatus();
}
}
第五步:配置应用程序
配置application.properties
文件以适应您的环境需求。
第六步:运行应用程序
在两台虚拟机(20.12和20.16)上启动Spring Boot应用程序,并通过RESTful接口来控制文件监听服务。
启动服务命令
在项目根目录,运行以下命令来启动Spring Boot应用程序:
mvn spring-boot:run
现在,您可以通过以下接口控制文件监听:
POST /watcher/start?dirPath=<directoryPath>
: 开始监听指定目录POST /watcher/stop?dirPath=<directoryPath>
: 停止监听指定目录GET /watcher/status
: 获取当前所有监听目录的状态
通过这些接口,您可以动态地启动和停止对不同目录的监听,并根据需要获取当前监听状态。
上面的功能实现监听多个不同目录文件怎么实现
为了实现对多个不同目录的文件进行监听,可以在FileWatcherService
类中扩展逻辑,使其支持同时监听多个目录。我们需要确保每个目录都能被单独管理和监听。以下是详细的实现步骤:
第一步:修改FileWatcherService
以支持多目录监听
package com.example.filewatcher.service;
import com.github.drapostolos.rwscf.FileSystemChangeObserver;
import com.github.drapostolos.rwscf.FileSystemChangeWatcher;
import com.github.drapostolos.rwscf.OnFileChange;
import com.github.drapostolos.rwscf.predicate.Filters;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class FileWatcherService {
private final Map<String, FileSystemChangeWatcher> watchers = new HashMap<>();
@PostConstruct
public void init() {
// Initialization code if needed
}
public void startWatching(String dirPath) throws IOException {
if (watchers.containsKey(dirPath)) {
return; // Already watching this directory
}
Path path = Paths.get(dirPath);
FileSystemChangeWatcher watcher = FileSystemChangeWatcher.newBuilder()
.addObserver(new FileSystemChangeObserver() {
@Override
@OnFileChange
public void onChange(Path changedFilePath) {
try {
// Handle file change
Files.copy(changedFilePath, Paths.get("/data/file/" + changedFilePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
stopWatching(dirPath); // Stop watching this directory after file is copied
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addListenerFilters(Filters.every(5, TimeUnit.SECONDS))
.path(path)
.build();
watcher.start();
watchers.put(dirPath, watcher);
}
public void stopWatching(String dirPath) {
FileSystemChangeWatcher watcher = watchers.remove(dirPath);
if (watcher != null) {
watcher.stop();
}
}
public Map<String, String> getStatus() {
Map<String, String> status = new HashMap<>();
watchers.forEach((dirPath, watcher) -> {
status.put(dirPath, watcher.isRunning() ? "Running" : "Stopped");
});
return status;
}
}
第二步:修改控制器以支持多目录监听
确保FileWatcherController
能够处理多个目录的监听请求。
package com.example.filewatcher.controller;
import com.example.filewatcher.service.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;
@RestController
@RequestMapping("/watcher")
public class FileWatcherController {
@Autowired
private FileWatcherService fileWatcherService;
@PostMapping("/start")
public Map<String, String> startWatching(@RequestParam String dirPath) throws IOException {
fileWatcherService.startWatching(dirPath);
return fileWatcherService.getStatus();
}
@PostMapping("/stop")
public Map<String, String> stopWatching(@RequestParam String dirPath) {
fileWatcherService.stopWatching(dirPath);
return fileWatcherService.getStatus();
}
@GetMapping("/status")
public Map<String, String> getStatus() {
return fileWatcherService.getStatus();
}
}
第三步:添加异常处理
为了更好地处理异常和错误,添加一个全局异常处理器。
package com.example.filewatcher.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(IOException.class)
public ResponseEntity<String> handleIOException(IOException ex) {
return new ResponseEntity<>("IO Error: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception ex) {
return new ResponseEntity<>("Error: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
第四步:启动和测试服务
在两台虚拟机上启动Spring Boot应用程序,通过RESTful接口控制文件监听服务:
-
启动服务:
mvn spring-boot:run
-
启动监听:
curl -X POST "http://localhost:8080/watcher/start?dirPath=/path/to/directory1" curl -X POST "http://localhost:8080/watcher/start?dirPath=/path/to/directory2"
-
停止监听:
curl -X POST "http://localhost:8080/watcher/stop?dirPath=/path/to/directory1"
-
获取监听状态:
curl -X GET "http://localhost:8080/watcher/status"
通过这些接口,你可以动态地启动和停止对不同目录的监听,并根据需要获取当前的监听状态。这样就实现了同时监听多个不同目录文件的功能。
用java1.8的maven的springboot项目在20.12和20.16两台虚拟机分别启动服务同时监听多个目录下的不同的txt文件,如果监听到了一个目录下的文件,就把文件复制到/data/file里然后结束这个目录文件的监听,继续监听其他目录文件,通过接口调用开启监听返回监听状态,通过接口控制停止监听返回监听状态
好的,这里是一个完整的实现方案,使用Java 1.8、Maven和Spring Boot,在两台虚拟机上分别启动服务,并监听多个目录下的不同的TXT文件。如果监听到了一个目录下的文件,就把文件复制到 /data/file
目录,然后结束这个目录文件的监听,继续监听其他目录文件。通过接口调用可以开启监听并返回监听状态,通过接口控制停止监听并返回监听状态。
项目结构
.
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── filewatcher
│ │ │ ├── FileWatcherApplication.java
│ │ │ ├── controller
│ │ │ │ └── FileWatcherController.java
│ │ │ └── service
│ │ │ └── FileWatcherService.java
│ │ └── resources
│ │ └── application.properties
└── pom.xml
1. pom.xml
首先,添加必要的依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>filewatcher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.drapostolos</groupId>
<artifactId>rwscf</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. FileWatcherApplication.java
package com.example.filewatcher;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FileWatcherApplication {
public static void main(String[] args) {
SpringApplication.run(FileWatcherApplication.class, args);
}
}
3. FileWatcherService.java
package com.example.filewatcher.service;
import com.github.drapostolos.rwscf.FileSystemChangeObserver;
import com.github.drapostolos.rwscf.FileSystemChangeWatcher;
import com.github.drapostolos.rwscf.OnFileChange;
import com.github.drapostolos.rwscf.predicate.Filters;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class FileWatcherService {
private final Map<String, FileSystemChangeWatcher> watchers = new HashMap<>();
@PostConstruct
public void init() {
// Initialization code if needed
}
public void startWatching(String dirPath) throws IOException {
if (watchers.containsKey(dirPath)) {
return; // Already watching this directory
}
Path path = Paths.get(dirPath);
FileSystemChangeWatcher watcher = FileSystemChangeWatcher.newBuilder()
.addObserver(new FileSystemChangeObserver() {
@Override
@OnFileChange
public void onChange(Path changedFilePath) {
try {
if (changedFilePath.toString().endsWith(".txt")) {
// Handle file change
Files.copy(changedFilePath, Paths.get("/data/file/" + changedFilePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
stopWatching(dirPath); // Stop watching this directory after file is copied
}
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addListenerFilters(Filters.every(5, TimeUnit.SECONDS))
.path(path)
.build();
watcher.start();
watchers.put(dirPath, watcher);
}
public void stopWatching(String dirPath) {
FileSystemChangeWatcher watcher = watchers.remove(dirPath);
if (watcher != null) {
watcher.stop();
}
}
public Map<String, String> getStatus() {
Map<String, String> status = new HashMap<>();
watchers.forEach((dirPath, watcher) -> {
status.put(dirPath, watcher.isRunning() ? "Running" : "Stopped");
});
return status;
}
}
4. FileWatcherController.java
package com.example.filewatcher.controller;
import com.example.filewatcher.service.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;
@RestController
@RequestMapping("/watcher")
public class FileWatcherController {
@Autowired
private FileWatcherService fileWatcherService;
@PostMapping("/start")
public Map<String, String> startWatching(@RequestParam String dirPath) throws IOException {
fileWatcherService.startWatching(dirPath);
return fileWatcherService.getStatus();
}
@PostMapping("/stop")
public Map<String, String> stopWatching(@RequestParam String dirPath) {
fileWatcherService.stopWatching(dirPath);
return fileWatcherService.getStatus();
}
@GetMapping("/status")
public Map<String, String> getStatus() {
return fileWatcherService.getStatus();
}
}
5. 配置文件 application.properties
properties
# 配置文件,可以根据需要进行配置
6. 启动和测试服务
启动服务
在项目根目录运行以下命令来启动Spring Boot应用程序:
mvn spring-boot:run
测试接口
-
启动监听:
curl -X POST "http://localhost:8080/watcher/start?dirPath=/path/to/directory1" curl -X POST "http://localhost:8080/watcher/start?dirPath=/path/to/directory2"
-
停止监听:
bash
curl -X POST "http://localhost:8080/watcher/stop?dirPath=/path/to/directory1"
-
获取监听状态:
bash
curl -X GET "http://localhost:8080/watcher/status"
通过这些接口,你可以动态地启动和停止对不同目录的监听,并根据需要获取当前的监听状态。这样就实现了同时监听多个不同目录文件的功能。确保你的虚拟机有权限访问和写入/data/file
目录,并且目录路径是正确的。
上面的功能的不通文件名称在哪里传递
要实现监听多个目录并且在监听到特定文件后进行相应操作,我们可以进一步修改代码,使得文件名称也可以作为参数传递。这样,我们可以灵活地根据不同目录和文件名称进行监听和处理。
目录和文件监听实现
1. 修改 FileWatcherService
以支持传递文件名称
首先,我们修改 FileWatcherService
类,使其能够接收文件名称参数。
package com.example.filewatcher.service;
import com.github.drapostolos.rwscf.FileSystemChangeObserver;
import com.github.drapostolos.rwscf.FileSystemChangeWatcher;
import com.github.drapostolos.rwscf.OnFileChange;
import com.github.drapostolos.rwscf.predicate.Filters;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class FileWatcherService {
private final Map<String, FileSystemChangeWatcher> watchers = new HashMap<>();
@PostConstruct
public void init() {
// Initialization code if needed
}
public void startWatching(String dirPath, String fileName) throws IOException {
String key = dirPath + ":" + fileName;
if (watchers.containsKey(key)) {
return; // Already watching this directory and file
}
Path path = Paths.get(dirPath);
FileSystemChangeWatcher watcher = FileSystemChangeWatcher.newBuilder()
.addObserver(new FileSystemChangeObserver() {
@Override
@OnFileChange
public void onChange(Path changedFilePath) {
try {
if (changedFilePath.getFileName().toString().equals(fileName)) {
// Handle file change
Files.copy(changedFilePath, Paths.get("/data/file/" + changedFilePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
stopWatching(dirPath, fileName); // Stop watching this directory and file after file is copied
}
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addListenerFilters(Filters.every(5, TimeUnit.SECONDS))
.path(path)
.build();
watcher.start();
watchers.put(key, watcher);
}
public void stopWatching(String dirPath, String fileName) {
String key = dirPath + ":" + fileName;
FileSystemChangeWatcher watcher = watchers.remove(key);
if (watcher != null) {
watcher.stop();
}
}
public Map<String, String> getStatus() {
Map<String, String> status = new HashMap<>();
watchers.forEach((key, watcher) -> {
status.put(key, watcher.isRunning() ? "Running" : "Stopped");
});
return status;
}
}
2. 修改 FileWatcherController
以支持传递文件名称
然后,我们修改 FileWatcherController
类,使其能够通过RESTful接口接收文件名称参数。
package com.example.filewatcher.controller;
import com.example.filewatcher.service.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;
@RestController
@RequestMapping("/watcher")
public class FileWatcherController {
@Autowired
private FileWatcherService fileWatcherService;
@PostMapping("/start")
public Map<String, String> startWatching(@RequestParam String dirPath, @RequestParam String fileName) throws IOException {
fileWatcherService.startWatching(dirPath, fileName);
return fileWatcherService.getStatus();
}
@PostMapping("/stop")
public Map<String, String> stopWatching(@RequestParam String dirPath, @RequestParam String fileName) {
fileWatcherService.stopWatching(dirPath, fileName);
return fileWatcherService.getStatus();
}
@GetMapping("/status")
public Map<String, String> getStatus() {
return fileWatcherService.getStatus();
}
}
3. 示例接口调用
通过RESTful接口,我们可以传递目录路径和文件名称来启动和停止监听:
-
启动监听(指定目录和文件名称):
curl -X POST "http://localhost:8080/watcher/start?dirPath=/path/to/directory1&fileName=example.txt" curl -X POST "http://localhost:8080/watcher/start?dirPath=/path/to/directory2&fileName=example2.txt"
-
停止监听(指定目录和文件名称):
curl -X POST "http://localhost:8080/watcher/stop?dirPath=/path/to/directory1&fileName=example.txt"
-
获取监听状态:
curl -X GET "http://localhost:8080/watcher/status"
通过这些接口,你可以动态地启动和停止对不同目录和文件的监听,并根据需要获取当前的监听状态。确保虚拟机有权限访问和写入 /data/file
目录,并且目录路径和文件名称是正确的。
上面的功能实现每隔linux上同时监听多个路径的不通文件名称怎么实现
为了在Linux上同时监听多个路径的不同文件名称,我们可以扩展现有的代码,使其支持多个路径和文件名的监听。在这个例子中,我们会将路径和文件名作为参数传递给服务,并使用一个数据结构来管理这些路径和文件名的监听状态。
1. 修改 FileWatcherService
以支持多个路径和文件名
我们需要扩展 FileWatcherService
类,使其能够同时监听多个路径和文件名。
package com.example.filewatcher.service;
import com.github.drapostolos.rwscf.FileSystemChangeObserver;
import com.github.drapostolos.rwscf.FileSystemChangeWatcher;
import com.github.drapostolos.rwscf.OnFileChange;
import com.github.drapostolos.rwscf.predicate.Filters;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class FileWatcherService {
private final Map<String, FileSystemChangeWatcher> watchers = new HashMap<>();
@PostConstruct
public void init() {
// Initialization code if needed
}
public void startWatching(String dirPath, String fileName) throws IOException {
String key = dirPath + ":" + fileName;
if (watchers.containsKey(key)) {
return; // Already watching this directory and file
}
Path path = Paths.get(dirPath);
FileSystemChangeWatcher watcher = FileSystemChangeWatcher.newBuilder()
.addObserver(new FileSystemChangeObserver() {
@Override
@OnFileChange
public void onChange(Path changedFilePath) {
try {
if (changedFilePath.getFileName().toString().equals(fileName)) {
// Handle file change
Files.copy(changedFilePath, Paths.get("/data/file/" + changedFilePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
stopWatching(dirPath, fileName); // Stop watching this directory and file after file is copied
}
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addListenerFilters(Filters.every(5, TimeUnit.SECONDS))
.path(path)
.build();
watcher.start();
watchers.put(key, watcher);
}
public void stopWatching(String dirPath, String fileName) {
String key = dirPath + ":" + fileName;
FileSystemChangeWatcher watcher = watchers.remove(key);
if (watcher != null) {
watcher.stop();
}
}
public Map<String, String> getStatus() {
Map<String, String> status = new HashMap<>();
watchers.forEach((key, watcher) -> {
status.put(key, watcher.isRunning() ? "Running" : "Stopped");
});
return status;
}
}
2. 修改 FileWatcherController
以支持传递多个路径和文件名
接下来,我们需要修改 FileWatcherController
类,使其能够通过RESTful接口接收多个路径和文件名参数。
package com.example.filewatcher.controller;
import com.example.filewatcher.service.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/watcher")
public class FileWatcherController {
@Autowired
private FileWatcherService fileWatcherService;
@PostMapping("/start")
public Map<String, String> startWatching(@RequestParam List<String> dirPaths, @RequestParam List<String> fileNames) throws IOException {
if (dirPaths.size() != fileNames.size()) {
throw new IllegalArgumentException("The number of directories and file names must be the same.");
}
for (int i = 0; i < dirPaths.size(); i++) {
fileWatcherService.startWatching(dirPaths.get(i), fileNames.get(i));
}
return fileWatcherService.getStatus();
}
@PostMapping("/stop")
public Map<String, String> stopWatching(@RequestParam List<String> dirPaths, @RequestParam List<String> fileNames) {
if (dirPaths.size() != fileNames.size()) {
throw new IllegalArgumentException("The number of directories and file names must be the same.");
}
for (int i = 0; i < dirPaths.size(); i++) {
fileWatcherService.stopWatching(dirPaths.get(i), fileNames.get(i));
}
return fileWatcherService.getStatus();
}
@GetMapping("/status")
public Map<String, String> getStatus() {
return fileWatcherService.getStatus();
}
}
3. 示例接口调用
通过RESTful接口,我们可以传递多个目录路径和文件名来启动和停止监听:
-
启动监听(指定多个目录和文件名称):
curl -X POST "http://localhost:8080/watcher/start" -d "dirPaths=/path/to/directory1&dirPaths=/path/to/directory2&fileNames=example1.txt&fileNames=example2.txt"
-
停止监听(指定多个目录和文件名称):
curl -X POST "http://localhost:8080/watcher/stop" -d "dirPaths=/path/to/directory1&dirPaths=/path/to/directory2&fileNames=example1.txt&fileNames=example2.txt"
-
获取监听状态:
curl -X GET "http://localhost:8080/watcher/status"
通过这些接口,你可以动态地启动和停止对不同目录和文件的监听,并根据需要获取当前的监听状态。确保虚拟机有权限访问和写入 /data/file
目录,并且目录路径和文件名称是正确的。
这套代码架构支持多个目录和文件的监听,使得文件管理更加灵活和高效。