0
我試圖將一個自定義的(ProjectData)Java對象存儲在FTP文件中。 ProjectData類實現Serializable接口。我使用apache Commons Net FTP客戶端。我有一個自定義類FtpClient,它具有Commons net FTP Client的實例。我使用這個類連接,上傳文件,下載文件,storeFileStream。當我嘗試調用 saveProjectData函數時,它在執行save.writeObject(data)時拋出異常。在FTP中存儲ObjectOutputStream會導致java.net.SocketException:軟件導致連接中止:套接字寫入錯誤
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]
那麼,什麼是在代碼中的問題,我準備好了嗎該連接已經關閉,節省object.But的同時,我找不到地方的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;
}
}