FTP连接中断不抛出异常

时间:2013-05-01 04:26:14

标签: java exception exception-handling ftp apache-commons

我正在尝试创建一个ftp下载器,它会从最后读取文件的位置重新启动。 我将为此存储一些元数据。但在测试时,我正在踢出客户端并断开服务器连接。但是句柄没有进入特殊情况,如代码中所示:

package fileresumes;

/**
 *
 * @author agarwall
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;

public class FileRes {

    public static void main(String[] args) {

        FTPClient client = new FTPClient();
        FileOutputStream fos = null;
        int totalBytesRead = 0;

        try {
            client.connect("localhost");
            client.login("anonymous", "");
            // The remote filename to be downloaded. 
            String filename = "testing.txt";
            fos = new FileOutputStream(filename);

            boolean append = false;
            int offset = 0;
            long last_modified = 0;
            int size = 0;

            //long ro = client.getRestartOffset();
            //ro = client.getRestartOffset();
            //Download file from FTP server;

            final File file = new File("C:/users/deadman/Desktop/", "testing.txt");

            if (file.exists()) {
                last_modified = file.lastModified();       // lastModified() returns the milliseconds since 1970-01-01
                append = true;
                // Read offset from meta-data file 
                size = (int) file.length();
                offset = size;
            }

            //Setting the offset to resume download
            client.setRestartOffset(offset);
            InputStream inputFileStream;

            inputFileStream = client.retrieveFileStream("/large.txt");
            int bytesRead = 0;

            try {

                OutputStream out = new FileOutputStream(file, append);
                final byte[] bytes = new byte[1024];

                while ((bytesRead = inputFileStream.read(bytes)) != -1) {
                    out.write(bytes, 0, bytesRead);
                    totalBytesRead += bytesRead;
                }
                inputFileStream.close();
                out.flush();

                int get_reply_code = client.getReplyCode();

                System.out.println(get_reply_code);

            } catch (IOException e) {

                // I want my metadata to be updated here .

                System.out.println("IOException");

            } catch (RuntimeException e) {
                // I want my metadata to be updated here . 
                System.out.println("Runtime Exception ");
            } finally {
                try {

                    int get_reply_code = client.getReplyCode();
                    System.out.println(get_reply_code);

                    if (fos != null) {
                        fos.close();
                    }
                    client.disconnect();
                    System.out.println("finish");
                } catch (IOException e) {
                }
            }
        } catch (Exception e) {
        }
    }
}

在连接断开的情况下,任何人都可以帮助我,我们如何在这里处理异常。

1 个答案:

答案 0 :(得分:-1)

我一直在尝试做类似的事情,到目前为止我唯一能做的就是尝试另一个命令。我还没有接受任何编码转移,但对我而言,如果我登录并且有超时,被踢等等,如果我尝试更改文件夹(或类似的东西),则会抛出FTPConnectionClosedException。我浏览过谷歌,并在这里发布了一个问题,看看是否有更好的方法,但到目前为止还没有听到任何消息。