无法在Apache Wicket中序列化FTPClient

无法在Apache Wicket中序列化FTPClient,wicket,apache-commons,serializable,ftp-client,apache-commons-net,Wicket,Apache Commons,Serializable,Ftp Client,Apache Commons Net,我正在使用ApacheWicket和ApacheCommonsNet。 但当我定义 new FTPClient(); //apache commons net library 我有个例外 org.apache.commons.net.ftp.FTPClient at.erpel.as2connector.testtool.protocols.FTP.client [class=org.apache.commons.net.ftp.FTPClient] <----- field that

我正在使用ApacheWicket和ApacheCommonsNet。 但当我定义

new FTPClient(); //apache commons net library
我有个例外

org.apache.commons.net.ftp.FTPClient at.erpel.as2connector.testtool.protocols.FTP.client
[class=org.apache.commons.net.ftp.FTPClient] <----- field that is not serializable
org.apache.commons.net.ftp.FTPClient at.erpel.as2connector.testtool.protocols.ftp.client

[class=org.apache.commons.net.ftp.FTPClient]我不认为在多个请求上存储
FTPClient
实例是一个好主意。您应该创建
FTPClient
,使用它,然后立即丢弃它。这意味着您可以将它存储在局部变量中,而不必担心它不可序列化。(顺便说一句,这很有意义,因为它有一个复杂的状态,包括活动的TCP连接。)

我不认为在多个请求上存储
FTPClient
实例是一个好主意。您应该创建
FTPClient
,使用它,然后立即丢弃它。这意味着您可以将它存储在局部变量中,而不必担心它不可序列化。(顺便说一句,这很有意义,因为它有一个复杂的状态,包括活动的TCP连接。)

谢谢您的建议

我还用两种可能性解决了这个问题:

1) 您可以使该字段变为瞬态:

transient FTPClient() client;
2) 创建将使用FTPClient的父类的单例

public class AnyClass implements Serializable {

    private static AnyClass instance;

    private AnyClass() {

    }

    public static AnyClass getInstance() {
        if (instance == null) {
            instance = new AnyClass();
        }
        return instance;
    }

    FTPClient client = new FTPClient();
    ...
}

3) 正如biziclop所建议的:只为FTP通信创建自己的类

谢谢你的建议

我还用两种可能性解决了这个问题:

1) 您可以使该字段变为瞬态:

transient FTPClient() client;
2) 创建将使用FTPClient的父类的单例

public class AnyClass implements Serializable {

    private static AnyClass instance;

    private AnyClass() {

    }

    public static AnyClass getInstance() {
        if (instance == null) {
            instance = new AnyClass();
        }
        return instance;
    }

    FTPClient client = new FTPClient();
    ...
}
3) 正如biziclop所建议的:只为FTP通信创建自己的类

检查此问题:检查此问题: