FTPClient:sun.net.ftp.FtpClient由于种种原因,上传和下载均有失败的可能性,并且这种情况我也遇到了。我用Timer来定时下载文件,当循环次数多了之后,连接就会出现问题: public void download1(String remoteFile, String localFile,
JTextArea textarea) {
try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
os.close();
is.close();
} catch (IOException ex) {
ex.printStackTrace();
textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
textarea.setCaretPosition(textarea.getText().length());
Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
upload_DownloadFTP.download1(remoteFile, localFile, textarea);
Down d = new Down();
d.timer.cancel();
d.begin(textarea, Test.defaultSavepath);
}
}这个是我下载文件的方法。当出现异常时,这里能够捕获到这个异常,也能够显示“更新失败,正在尝试”。但是再往下程序就停住了。注意:“download1”方法在“Upload_DownloadFTP”这个类中。下面是我开始计时器的方法: public void begin(JTextArea textarea, String str) {
this.timer = new Timer();
this.timer.schedule(new MyTask(textarea, str), 1000, 100 * 60 * 15);
}因为在这里,我每次都要new一个timer对象出来,所以不存在Timer is already calceled这个错误,至少本人是这样认为。
那么为什么我写在catch里的语句就不会执行呢?提示到“验证更新失败,正在尝试重新运行程序...”就不执行了?

解决方案 »

  1.   

    还有一个很严重的问题就是在循环后我连接服务器的端口是越来越多,多的时候有7个连接端口。而我每次都进行了连接的关闭,另外,在每次连接前,我要查看一下ftpClient这个对象是否为空,如果不为空,那么就关闭连接,并把它赋值为空。下面是我建立连接与关闭连接的代码: public void connectServer1(String ip, int port, String user,
    String password, String path) {
    try {
    if (ftpClient != null) {
    ftpClient.closeServer();
    ftpClient = null;
    }
    ftpClient = new FtpClient();
    setConnectTimeOut(5000);
    ftpClient.openServer(ip, port);
    ftpClient.login(user, password);
    if (path.length() != 0)
    ftpClient.cd(path);
    ftpClient.binary();
    } catch (IOException ex) {
    System.out.println(ex);
    }
    } public void closeConnect1() {
    try {
    if (ftpClient != null) {
    ftpClient.closeServer();
    }
    } catch (IOException ex) {
    System.out.println("not disconnect");
    System.out.println(ex);
    } finally {
    ftpClient = null;
    }
    }
    愁死我了!
      

  2.   

    DDDDDDDDDDDDDDDDDDDDDDDD不要看了代码不回答问题,各位~~~~~~~~~
      

  3.   

    在 finally里面close  连接
      

  4.   

    ry {
                TelnetInputStream is = ftpClient.get(remoteFile);
                java.io.File file_in = new java.io.File(localFile);
                FileOutputStream os = new FileOutputStream(file_in);
                byte[] bytes = new byte[1024];
                int c;
                while ((c = is.read(bytes)) != -1) {
                    // System.out.println((char)is.read());
                    // System.out.println(file_in);
                    os.write(bytes, 0, c);
                }
                os.close();
                is.close();
            } catch (IOException ex) {
                ex.printStackTrace();
                textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
                textarea.setCaretPosition(textarea.getText().length());
                Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
                upload_DownloadFTP.download1(remoteFile, localFile, textarea);
                Down d = new Down();
                d.timer.cancel();
                d.begin(textarea, Test.defaultSavepath);
            }
    ========如果在os 和 is close 之前出现异常,那么 os /is 都不会close ,,
      

  5.   

    那么请问下:如果在close()之前出现了一场,我把这个os.close()和is.close();写在finally里,是先执行catch中的代码吧?如果这样的话,我在重复调用download1这个方法的时候,流还是在开启的状态啊。不知道可不可以这么理解?
      

  6.   


    public void download1(String remoteFile, String localFile,
    JTextArea textarea) {
    FileOutputStream os = null;
    TelnetInputStream is = null;
    try {
    is = ftpClient.get(remoteFile);
    java.io.File file_in = new java.io.File(localFile);
    os = new FileOutputStream(file_in);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = is.read(bytes)) != -1) {
    // System.out.println((char)is.read());
    // System.out.println(file_in);
    os.write(bytes, 0, c);
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
    textarea.setCaretPosition(textarea.getText().length());
    Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
    upload_DownloadFTP.download1(remoteFile, localFile, textarea);
    Down d = new Down();
    d.timer.cancel();
    d.begin(textarea, Test.defaultSavepath);
    } finally {
    if (os != null) {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if (is != null) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }照您说的改了就成了这样了。(我不喜欢用throws)
      

  7.   

    每次都new一个timer对象出来,这样我总觉得怪怪的,timer作为全局不行么?每次都new产生新的对象,资源也消耗大啊.个人觉得一个定时器应该可以完成你的需求的.(至于问题是不是出在这里就不知道了,尽供参考)
      

  8.   

    你好,我在new之前是要cancel的。cancel这段代码写在catch中。我的timer对象是设为static的,所以cancel可以关掉当前的计时器。这点应该没问题吧?
      

  9.   

    顶一下,能否在catch中直接调用download1方法?