gpt4 book ai didi

java - Java中文件监控程序检测失败

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:38 25 4
gpt4 key购买 nike

我的程序中有一个目录监控功能,用于监控任何新文件到达该专用目录。如果有任何新文件到达该文件夹,它将触发API调用并通过post方法将文件传递到远程服务器。出于测试目的,我将复制该文件并将其粘贴到我的测试目录中。但是当我检查我的服务器时,我发现至少有 5 个相似的文件在同一时间戳内到达了我的服务器,但我只将一个文件复制到我的测试目录。文件发送到远程服务器后,它会将文件移动到另一个目录,但在文件移动到另一个目录之前,远程服务器将返回一些 JSON 数据处理,以检查哪个是最适合移动文件的文件夹。

这是我源代码的一部分

try(WatchService svc = FileSystems.getDefault().newWatchService()) 
{
Map<WatchKey, Path> keyMap = new HashMap<>();
Path path = Paths.get("C://REST API//SentFile"); //register watch event for any new file
keyMap.put(path.register(svc,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY),
path);

WatchKey wk ;
do
{
Thread.sleep(10); //halt the program for a while
wk = svc.take();
Path dir = keyMap.get(wk);
for(WatchEvent<?> event : wk.pollEvents())
{
WatchEvent.Kind<?> type = event.kind();
Path fileName = (Path)event.context();
System.out.println(" The new file :"+fileName); //print the new file name
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.0.27:8008/file"); //remote API URL
String ConvertFile = String.valueOf(fileName);
String filepath = "C://REST API//SentFile//";
String file = filepath.concat(ConvertFile); // the file directory
FileBody bin = new FileBody(new File(file)); // take the file from directory
HttpEntity reqEntity = MultipartEntityBuilder.create() // build require argument
.addPart("display_name", bin)
.build();
post.addHeader("filename", ConvertFile);
post.setEntity(reqEntity); // send via post method
HttpResponse response = client.execute(post); // receice response from remote server
Thread.sleep(6); //halt the program for a while for remote processing
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer bf = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null)
{
bf.append(line);
}
JSONObject obj = new JSONObject(bf.toString()); //read the data from remote response
String ID = obj.getString("data_id");
System.out.println("The data id from server :" +ID); //display the ID
FileDetect fd = new FileDetect();
fd.send(ID); //send the ID for another API call



这是我的 send() 方法源代码

private void send(String data_ID) throws IOException, JSONException, InterruptedException 
{
System.out.println("The Data ID is :" + data_ID); //display ID after the send()
String Url = "http://192.168.0.27:8008/file/";
String FinalURL = Url.concat(data_ID);
System.out.println("URL is : " + FinalURL); // the remote API URL
URL theUrl = new URL (FinalURL);
HttpURLConnection con = (HttpURLConnection) theUrl.openConnection(); //API call
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
if(responseCode == 200)
{
try
{
InputStream is = con.getInputStream();
BufferedReader read = new BufferedReader (new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String data = "" ;
while((data = read.readLine() ) != null )
{
buffer.append(data);
}

String JsonData = buffer.toString();
JSONObject jobj = new JSONObject(JsonData);
JSONObject scArr = jobj.getJSONObject("scan_results");
JSONObject fileInfo = jobj.getJSONObject("file_info");
int scan_result = scArr.getInt("scan_all_result_i");
String fileName = fileInfo.getString("display_name");
System.out.println("The file name :" + fileName); //display JSON data from server
System.out.println("The scan result : " +scan_result ); //display JSON data from server
if(scan_result == 0 ) //move the file base on result
{
String sour1 = "C://REST API//SentFile//";
String sourPath = sour1.concat(fileName);
try
{
Path sour = Paths.get(sourPath);
Path Dest = Paths.get("C://Users//user//Documents//no_thread");
System.out.println("Start to copy no_thread file");
Files.move(sour, Dest.resolve(sour.getFileName()));
System.out.println("Completed");
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if(scan_result == 1 ) //move the file base on result
{
String sour1 = "C://REST API//SentFile//";
String sourPath = sour1.concat(fileName);
try
{
Path sour = Paths.get(sourPath);
Path Dest = Paths.get("C://Users//user//Documents//infected");
System.out.println("Start to copy infected file");
Files.move(sour, Dest.resolve(sour.getFileName()));
System.out.println("Completed");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
finally
{
System.out.print("No Error");
}
}
Thread.sleep(10);
}
}



这是我的编译器中显示的调试语句

 The new file :eicar.com.txt
The data id from server :86ade39d4b7243188245bc8537242fa4
The Data ID is :86ade39d4b7243188245bc8537242fa4
URL is : http://192.168.0.27:8008/file/86ade39d4b7243188245bc8537242fa4
The file name :eicar.com.txt
The scan result : 1
Start to copy infected file
Completed
No Error
The new file :eicar.com.txt
Dec 03, 2016 5:37:19 PM org.apache.http.impl.client.DefaultRequestDirector execute
INFO: I/O exception (java.io.FileNotFoundException) caught when processing request: C:\REST API\SentFile\eicar.com.txt (The system cannot find the file specified)
Dec 03, 2016 5:37:19 PM org.apache.http.impl.client.DefaultRequestDirector execute
INFO: Retrying request
Dec 03, 2016 5:37:19 PM org.apache.http.impl.client.DefaultRequestDirector execute
INFO: I/O exception (java.io.FileNotFoundException) caught when processing request: C:\REST API\SentFile\eicar.com.txt (The system cannot find the file specified)
Dec 03, 2016 5:37:19 PM org.apache.http.impl.client.DefaultRequestDirector execute
INFO: Retrying request
Dec 03, 2016 5:37:19 PM org.apache.http.impl.client.DefaultRequestDirector execute
INFO: I/O exception (java.io.FileNotFoundException) caught when processing request: C:\REST API\SentFile\eicar.com.txt (The system cannot find the file specified)
Dec 03, 2016 5:37:19 PM org.apache.http.impl.client.DefaultRequestDirector execute
INFO: Retrying request
Problem io in somewhere



从编译器显示来看,从一开始一切都很好,但是“新文件:eicar.com.txt”在“无错误”打印语句之后第二次显示,我有程序在“无错误”打印语句显示之前将文件移动到另一个目录,但我不知道为什么它能够检测到 eicar.com.txt 在我的目录中仍然是一个新文件,我将 thread.sleep() 在我的程序中,但它失败了按预期运行

最佳答案

您需要重置您的WatchKey,然后才能再次拍摄:

wk = svc.take();
//Whatever you need to do here
wk.reset();

关于java - Java中文件监控程序检测失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40946713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com