请教各位大侠:如何用java监控ftp服务器的特定文件夹是否有新文件上传??

gshwu 2004-06-16 11:32:21
如题。
...全文
1140 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hykwolf 2004-06-21
  • 打赏
  • 举报
回复
一个本地监听的大体思路

File f = new File(root) //root,要查看的目录
while(true){
String[] fn = f.list(); //获取所有子File对象
File tmp = null;
for(int i=0;i<fn.length();i++){
tmp = new File(fn[i])
if(tmp.isDirectory()){
继续朝下走两步
}else{
是文件了..入库入库,查查看以前有没有
if(o,new.file 新文件){
新来的,整吧....
}
}
}
//扫完了...太累了,改休息一下了
try{
Thread.sleep(1000*60); //让俺睡一分钟吧..整的累了
}catch(Exception e){}
}
weil 2004-06-21
  • 打赏
  • 举报
回复
支持 Arias(永恒之道) ( ) 的说法。
leslie0110 2004-06-21
  • 打赏
  • 举报
回复
/*
* Created on 2004/06/17
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package playoffs.test.ftp;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import sun.net.ftp.FtpClient;
import sun.net.www.protocol.ftp.FtpURLConnection;

/**
* @author wangji
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FtpTest {

private FtpURLConnection ftpConn;
private FtpClient ftpClient;
private URL url;

public FtpTest(){

try {
ftpClient = new FtpClient("xxx.xxx.xxx.xxx");
} catch (IOException e) {
e.printStackTrace();
}
try {
ftpClient.login("xxxxx","xxxxxx");
} catch (IOException e2) {
e2.printStackTrace();
}
try {
ftpClient.ascii();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
ftpClient.cd("xxxx/log");
} catch (IOException e3) {
e3.printStackTrace();
}
InputStream in = null;
try {
in = ftpClient.list();
} catch (IOException e4) {
e4.printStackTrace();
}
int x = 0;
try {
x = in.read();
} catch (IOException e5) {
e5.printStackTrace();
}
while(x!=-1){
System.out.print((char)x);
try {
x = in.read();
} catch (IOException e6) {
e6.printStackTrace();
}
}
// System.out.println(ftpClient.list().toString());
String str = ftpClient.getResponseString();
System.out.println(str);
System.out.println("finish");
try {
url = new URL("ftp://xxx.xxx.xxx.x/xxxx/log/xxxxx.log");
} catch (MalformedURLException e6) {
e6.printStackTrace();
}
try {
ftpConn = (FtpURLConnection) url.openConnection();
} catch (IOException e9) {
e9.printStackTrace();
}
try {
InputStream reader = ftpConn.getInputStream();
} catch (IOException e7) {
e7.printStackTrace();
}
}

public static void main(String[] args){
new FtpTest();
}
}
Arias 2004-06-19
  • 打赏
  • 举报
回复
其实就是设置一个对文件夹中文件的检测嘛!
你每次把里面的文件名称全部存在一个容器里面(在java中有vector),然后获取及时的全部
文件名称与之比较,如果里面没有就是新的那!
gshwu 2004-06-19
  • 打赏
  • 举报
回复
没有更多的人知道该如何做吗?
happyegg 2004-06-16
  • 打赏
  • 举报
回复
用一个线程来监控这个folder,用一个文件或者数据库来记录你上一次扫描的时间,然后如果扫描到有文件的last modify time迟过你上一次扫描时间的话......

如果你每次扫描后把文件搬到另一个目录就更简单了......

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧