我正在尝试将自定义定义的 (ProjectData) Java 对象存储在 FTP 的文件中。ProjectData 类实现 Serializable 接口。我正在使用 apache Commons Net FTP 客户端。我有一个自定义类 FtpClient ,它具有 Commons net FTP 客户端的实例。我使用这个类连接、上传文件、下载文件、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]
那么代码中的问题是什么,我准备好连接已经关闭,同时保存对象。但是我找不到错误在哪里。
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;
}
}