在FTP中存储ObjectOutputStream会导致java.net.SocketException:软件导致连接中止:套接字写入错误

时间:2022-03-12 05:52:31

I am trying to store an custom defined (ProjectData) Java object in a file in FTP . ProjectData class implements Serializable interface.I am using apache Commons Net FTP client.I have a custom class FtpClient , which has instance of Commons net FTP Client. I connect , upload file, download file, storeFileStream using this class.When I try to call saveProjectData function, it throws exception while executing save.writeObject(data).

我试图在FTP中的文件中存储自定义(ProjectData)Java对象。 ProjectData类实现了Serializable接口。我正在使用apache Commons Net FTP客户端。我有一个自定义类FtpClient,它有Commons net FTP Client的实例。我使用这个类连接,上传文件,下载文件,storeFileStream。当我尝试调用saveProjectData函数时,它会在执行save.writeObject(data)时抛出异常。

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method) [:1.6.0_23]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) [:1.6.0_23]
at java.net.SocketOutputStream.write(SocketOutputStream.java:136) [:1.6.0_23]
at org.apache.commons.net.io.SocketOutputStream.write(SocketOutputStream.java:72) [:2.2]
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1847) [:1.6.0_23]
at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1792) [:1.6.0_23]
at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:699) [:1.6.0_23]

So what is the problem in the code, I ready that connection is already closed, in the meantime of saving object.But I could not find where the bug.

那么代码中的问题是什么,我准备连接已经关闭,同时保存对象。但是我找不到bug的位置。

public void saveProjectData(ProjectData data, String filePath) {
    ftpClient = new FtpClient(ftpConfig, logger);
    ObjectOutputStream save = null;
    try {
        ftpClient.connect();
        save = new ObjectOutputStream(ftpClient.storeFileStream(filePath));
    } catch (Exception e) {
        // ignore
    }
    try {
        if (save == null) {
            ftpClient.reconnect();
            save = new ObjectOutputStream(ftpClient.storeFileStream(filePath));
        }
        save.writeObject(data);
        save.flush();
    } catch (IOException e) {
        logger.error("Error creating file:" + filePath);
        throw new FileTransferException("Could not create file:" + filePath, e);
    } finally {
        if (save != null) {
            try {
                save.close();
            } catch (Exception e) {
                logger.warn("Error closing writer of file:" + filePath, e);
            }
            save = null;
        }
        if (ftpClient != null) {
            try {
                ftpClient.disconnect();
            } catch (Exception e) {
                // ignore
            }
            ftpClient = null;
        }
    }
}

    /**
 * Stores the file in FTP
 * @param filePath
 * @return
 */
public OutputStream storeFileStream(String filePath) {
    try {
        client.setFileType(FTP.BINARY_FILE_TYPE);
        return client.storeFileStream(filePath);
    } catch (Exception ioe) {
        log.warn("Could not store file:" + filePath, ioe);
        return null;
    }
}

1 个解决方案

#1


0  

I read that connection is already closed

我读到该连接已经关闭

No, you read (or you should have read) that the connection is already closed by the peer.

不,您阅读(或者您应该已阅读)该对等已经关闭了该连接。

Possibly you have exceeded an upload limit.

可能您已超出上传限制。

#1


0  

I read that connection is already closed

我读到该连接已经关闭

No, you read (or you should have read) that the connection is already closed by the peer.

不,您阅读(或者您应该已阅读)该对等已经关闭了该连接。

Possibly you have exceeded an upload limit.

可能您已超出上传限制。