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

时间:2013-01-07 13:20:22

标签: java sockets object serialization ftp-client

我正在尝试将自定义(ProjectData)Java对象存储在FTP中的文件中。 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]

那么代码中的问题是什么,我准备好连接已经关闭,同时保存对象。但是我找不到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 个答案:

答案 0 :(得分:0)

  

我看到该连接已经关闭

不,你读过(或者你应该已经读过)对等方已经关闭的连接。

可能您已超出上传限制。