0

我正在尝试制作一个 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) {
        }
    }
}

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

4

1 回答 1

-1

我一直在尝试做类似的事情,到目前为止,我唯一能做的就是尝试另一个命令。我还没有在编码中进行任何传输,但是对我来说,如果我登录并且出现超时、被踢等,如果我尝试更改文件夹(或类似的东西),它会抛出 FTPConnectionClosedException。我环顾了谷歌,并在这里发布了一个问题,看看是否有更好的方法,但到目前为止还没有听到任何消息。

于 2013-05-01T22:18:36.630 回答