[英]Store ObjectOutputStream in FTP causes java.net.SocketException: Software caused connection abort: socket write error
I am trying to store an custom defined (ProjectData) Java object in a file in FTP . 我试图将自定义定义的(ProjectData)Java对象存储在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.
ProjectData类实现Serializable接口。我使用的是Apache Commons Net FTP客户端。我有一个自定义类FtpClient,该类具有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).
我使用此类连接,上传文件,下载文件,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. 那么代码中的问题是什么,我准备在保存对象的同时关闭该连接,但是我找不到错误所在。
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;
}
}
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. 您可能超出了上传限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.