简体   繁体   English

FTP连接中断未引发异常

[英]FTP connection interruption Exception not thrown

I am trying to make a ftp downloader which would restart from the position where the file was lastly read. 我正在尝试制作一个ftp下载器,它将从最后读取文件的位置重新启动。 I will be storing some meta-data for this . 我将为此存储一些元数据。 But while testing i am kicking out the client and also disconnecting the server . 但是在测试时,我将踢出客户端并断开服务器连接。 But the handle is not getting into the exceptional as indicated in the code: 但是句柄并没有进入代码所示的异常状态:

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) {
        }
    }
}

Can anyone help me in case of broken connection how can we handle the exception here. 如果连接断开,谁能帮助我,我们如何在这里处理异常。

I have been trying to do something similar, and so far the only thing I have been able to do is to attempt another command. 我一直在尝试做类似的事情,到目前为止,我唯一能做的就是尝试另一个命令。 I haven't gotten to any transfers in coding yet, but for me if I login and there is either a timeout, get kicked, etc, if I try to change folders (or something similar) it throws a FTPConnectionClosedException. 我还没有进行任何编码方面的传输,但是对我来说,如果我登录并且超时,被踢等,如果我尝试更改文件夹(或类似内容),它将引发FTPConnectionClosedException。 I have looked around Google, and have posted a question on here to see if there is a better way, but so far have not heard anything. 我环顾了Google,并在此处发布了一个问题,以查看是否有更好的方法,但到目前为止尚未听到任何声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM