我的程序中将有一个目录监控功能,用于监控进入该专用目录的任何新文件。如果有任何新文件到达该文件夹,它将触发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”在“无错误”打印语句后第二次显示,我有程序在“No Error”打印语句显示之前文件将移动到另一个目录,但我不知道为什么它能够检测到我的目录中仍然是一个新文件的eicar.com.txt,我把 thread.sleep ()在我的程序中,但它无法按预期运行
答案 0 :(得分:0)
您需要先reset
WatchKey
take
才能再次wk = svc.take();
//Whatever you need to do here
wk.reset();
:
{{1}}