java ftp删除文件的问题

a252353632 2009-04-17 10:55:09
我没有积分,没办法给分了
代码如下,执行fc.sendServer(del) 的时候只能每次删除2、3个文件?问题何在?我执行的是删除7个文件的啊
public class ftpDown{
FtpClient fc;
boolean loginOK; //用来判断是否正常连接服务器

public static void main(String[] args){
Object[] fileList;
String[][] address = {
{"83.16.16.72", "oracle", "fxpg7065"},
{"83.16.21.121", "admin", "a123456"}
};
String[] dir = {"oracle\\iostat_log", "oracle\\vmstat_log"};
ftpDown fDown = new ftpDown();
//测试,所以暂且一次循环
for(int a = 1; a < 2; a++){
fDown.connect(address[a][0], address[a][1], address[a][2]);

if(fDown.loginOK){
System.out.println("***************************************");

for(int d = 1; d < 2; d++){
fileList = fDown.getFileList(dir[d]).toArray();
Arrays.sort(fileList); //文件排序,保证取得的文件是最老的那些
int dirLen = dir[d].length();
int count = 0;

//一周生成7个文件,用8来保证第七个文件已经写入完毕
if(fileList.length < 8){
System.out.println(" 还没有生够一周的文件(7个)");
}
else if(fileList.length > 7){
for(int i = 0; i < 7; i++){
boolean isOK = fDown.downLoadFile(dirLen, (String)fileList[i]);
if(isOK){ //如果成功下载
count++;
}
}
}

//如果7个文件成功下载本地,则删除服务器上的该文件
if(count == 7){
for(int i = 0; i < 7; i++){
fDown.deleteLoadFile((String)fileList[i]);
}
}
}
System.out.println("***************************************");
}
fDown.stop();
}
}

//获得fileDir目录下的所有文件列表
public ArrayList getFileList(String fileDir){
ArrayList aList = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(fc.nameList(fileDir)));
aList = new ArrayList();
String s = "";
while((s = br.readLine()) != null){
aList.add(s);
}
}catch (IOException e) {
System.out.println("获取文件列表出错");
}

return aList;
}

//下载文件
public boolean downLoadFile(int dirLen, String fullFileName){
boolean result = false;
if(fc != null){
try{
int ch;
String savePath = System.getProperty("user.dir");
String shotName = fullFileName.substring(dirLen + 1, fullFileName.length());
File fl = new File(savePath + "\\" + shotName);
RandomAccessFile getFile = new RandomAccessFile(fl, "rw");
getFile.seek(0);
TelnetInputStream fget = fc.get(fullFileName);
DataInputStream puts = new DataInputStream(fget);
System.out.print("正在下载 " + shotName + ",请等待...");
while((ch = puts.read()) >= 0){
getFile.write(ch);
}

fget.close();
getFile.close();
System.out.println(" 下载成功");
result = true;
}catch(IOException e){
result = false;
System.out.println(" 下载失败" + e);
}
}
else
result = false;

return result;
}

public void deleteLoadFile(String fileName){
if(fc != null){
String del = "DELE " + fileName + " \r\n";
System.out.print(del); //输出执行的命令
fc.sendServer(del);
}
}

public FtpClient connect(String hostName, String uid, String pwd){
System.out.println("正在连接 " + hostName + ",请等待...");
try {
fc =new FtpClient(hostName);
fc.login(uid, pwd);
fc.binary();
System.out.println("连接主机 " + hostName + " 成功");
loginOK = true;
}catch (FtpLoginException ef) {
System.out.println("请检查用户或密码是否正确!");
loginOK = false;
}catch (Exception en) {
System.out.println("无法与主机连接!");
loginOK = false;
}
return fc;
}

public void stop(){
try {
if(fc != null){
fc.closeServer();
System.out.println("与主机连接已断开");
}
} catch (IOException e) {
System.out.println("与主机断开失败" + e);
}
}
}
...全文
1490 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lk123456sc 2012-02-21
  • 打赏
  • 举报
回复
说错了
lk123456sc 2012-02-21
  • 打赏
  • 举报
回复
ftpDown 这个类在哪呢
bzray 2011-04-08
  • 打赏
  • 举报
回复
楼主,1楼是否正解?
FBI_CY 2010-01-20
  • 打赏
  • 举报
回复
太谢谢楼主啦!我正好遇见相同的问题!
a252353632 2009-04-20
  • 打赏
  • 举报
回复
fc.sendServer(del);后少加了一句
fc.readServerResponse();

58,452

社区成员

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

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