我正在使用org.apache.commons.net.ftp.FTPClient
从7kb文件下载和读取数据。它总是在同一行(大约是文件的50%)崩溃并出现“连接重置”异常。服务器说传输成功。
这肯定是与大小相关的东西,如果我用其他东西替换文本,它会在大约相同的位置崩溃,如果我从行开始删除所有文本,它就不会崩溃。
它也会(始终)发生在旧的远程ftp服务器以及我刚刚配置的不相关的本地服务器上。与成功服务器输出点相结合,以客户端为导向。
public boolean ftpConnect(String host, String username, String password, int port) {
try {
ftpClient = new FTPClient();
// ftpClient.setConnectTimeout(10000);
// connecting to the host
ftpClient.connect(host, port);
ftpClient.setKeepAlive(true);
ftpClient.setControlKeepAliveTimeout(100000);
ftpClient.setControlKeepAliveReplyTimeout(10000);
ftpClient.setConnectTimeout(100000);
ftpClient.setDataTimeout(10000);
ftpClient.setSoTimeout(100000);
// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
// login using username & password
boolean status = ftpClient.login(username, password);
/*
* Set File Transfer Mode
* To avoid corruption issue you must specified a correct
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE for
* transferring text, image, and compressed files.
*/
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
// ftpClient.setFileType(FTP.LOCAL_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
// ftpClient.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
ftpClient.enterLocalPassiveMode();
return status;
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + host + ": " + e.getMessage());
e.printStackTrace();
}
return false;
}
下载代码:
InputStream inputStream = null;
try {
inputStream = ftpClient.retrieveFileStream(myPath);
if (inputStream != null) {
Map<String, String> stuff= loadStuff(inputStream, languageDescriptor.code);
ftpClient.enterLocalPassiveMode();
return stuff;
} else {
Log.e(TAG, errorMessage);
return null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
}
}
private Map<String, String> loadStuff(InputStream inputStream) throws IllegalFormatException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
Map<String, String> stuff= new HashMap<>();
while ((line = reader.readLine()) != null) {
if (line.startsWith(UTF8_BOM)) {
line = line.substring(1);
}
if (line.trim().isEmpty() || line.startsWith("[")) {
continue;
} else {
// parse line, store in map
}
}
return stuff;
} catch (IOException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
}
}
}
服务器输出:
(000352)30.05.2017 15:09:50 - test (127.0.0.1)> RETR myfile.lng
(000352)30.05.2017 15:09:50 - test (127.0.0.1)> 150 Opening data channel for file download from server of "/myfile.lng"
(000352)30.05.2017 15:09:50 - test (127.0.0.1)> 226 Successfully transferred "/myfile.lng"
(000352)30.05.2017 15:09:50 - test (127.0.0.1)> QUIT
(000352)30.05.2017 15:09:50 - test (127.0.0.1)> 221 Goodbye
(000352)30.05.2017 15:09:50 - test (127.0.0.1)> disconnected.
修改 有人要求显示使用标准ftp客户端从ftp服务器下载文件的日志。我用过Filezilla。
与本地服务器连接:
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 0
16:29:10 Status: Connecting to 127.0.0.1:21...
16:29:10 Status: Connection established, waiting for welcome message...
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 220-FileZilla Server 0.9.60 beta
16:29:10 Response: 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
16:29:10 Response: 220 Please visit https://filezilla-project.org/
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 1
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 2
16:29:10 Command: AUTH TLS
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 502 Explicit TLS authentication not allowed
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 2
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 3
16:29:10 Command: AUTH SSL
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 502 Explicit TLS authentication not allowed
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 3
16:29:10 Status: Insecure server, it does not support FTP over TLS.
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 5
16:29:10 Command: USER test
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 331 Password required for test
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 5
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 5
16:29:10 Command: PASS *******
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 230 Logged on
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 5
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 6
16:29:10 Command: SYST
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 215 UNIX emulated by FileZilla
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 6
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpLogonOpData::Send() in state 7
16:29:10 Command: FEAT
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 211-Features:
16:29:10 Response: MDTM
16:29:10 Response: REST STREAM
16:29:10 Response: SIZE
16:29:10 Response: MLST type*;size*;modify*;
16:29:10 Response: MLSD
16:29:10 Response: UTF8
16:29:10 Response: CLNT
16:29:10 Response: MFMT
16:29:10 Response: EPSV
16:29:10 Response: EPRT
16:29:10 Response: 211 End
16:29:10 Trace: CFtpLogonOpData::ParseResponse() in state 7
16:29:10 Status: Logged in
16:29:10 Trace: Measured latency of 0 ms
16:29:10 Trace: CFtpControlSocket::ResetOperation(0)
16:29:10 Trace: CControlSocket::ResetOperation(0)
16:29:10 Trace: CFileZillaEnginePrivate::ResetOperation(0)
16:29:10 Status: Retrieving directory listing...
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpListOpData::ListSend() in state 0
16:29:10 Trace: CFtpChangeDirOpData::Send() in state 0
16:29:10 Trace: CFtpChangeDirOpData::Send() in state 1
16:29:10 Command: PWD
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 257 "/" is current directory.
16:29:10 Trace: CFtpChangeDirOpData::ParseResponse() in state 1
16:29:10 Trace: CFtpControlSocket::ResetOperation(0)
16:29:10 Trace: CControlSocket::ResetOperation(0)
16:29:10 Trace: CControlSocket::ParseSubcommandResult(0)
16:29:10 Trace: CFtpListOpData::SubcommandResult() in state 1
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpListOpData::ListSend() in state 2
16:29:10 Trace: CFtpRawTransferOpData::Send() in state 1
16:29:10 Command: TYPE I
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 200 Type set to I
16:29:10 Trace: CFtpRawTransferOpData::ParseResponse() in state 1
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpRawTransferOpData::Send() in state 2
16:29:10 Command: PASV
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 227 Entering Passive Mode (127,0,0,1,239,193)
16:29:10 Trace: CFtpRawTransferOpData::ParseResponse() in state 2
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpRawTransferOpData::Send() in state 4
16:29:10 Trace: Binding data connection source IP to control connection source IP 127.0.0.1
16:29:10 Command: MLSD
16:29:10 Trace: CTransferSocket::OnConnect
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 150 Opening data channel for directory listing of "/"
16:29:10 Trace: CFtpRawTransferOpData::ParseResponse() in state 4
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpRawTransferOpData::Send() in state 5
16:29:10 Trace: CFtpControlSocket::OnReceive()
16:29:10 Response: 226 Successfully transferred "/"
16:29:10 Trace: CFtpRawTransferOpData::ParseResponse() in state 5
16:29:10 Trace: CControlSocket::SendNextCommand()
16:29:10 Trace: CFtpRawTransferOpData::Send() in state 8
16:29:10 Trace: CTransferSocket::OnReceive(), m_transferMode=0
16:29:10 Trace: CTransferSocket::TransferEnd(1)
16:29:10 Trace: CFtpControlSocket::TransferEnd()
16:29:10 Trace: CFtpControlSocket::ResetOperation(0)
16:29:10 Trace: CControlSocket::ResetOperation(0)
16:29:10 Trace: CControlSocket::ParseSubcommandResult(0)
16:29:10 Trace: CFtpListOpData::SubcommandResult() in state 3
16:29:10 Trace: CFtpControlSocket::ResetOperation(0)
16:29:10 Trace: CControlSocket::ResetOperation(0)
16:29:10 Status: Directory listing of "/" successful
16:29:10 Trace: CFileZillaEnginePrivate::ResetOperation(0)
直接连接后 - 从本地服务器下载文件:
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpLogonOpData::Send() in state 0
16:30:49 Status: Connecting to 127.0.0.1:21...
16:30:49 Status: Connection established, waiting for welcome message...
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 220-FileZilla Server 0.9.60 beta
16:30:49 Response: 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
16:30:49 Response: 220 Please visit https://filezilla-project.org/
16:30:49 Trace: CFtpLogonOpData::ParseResponse() in state 1
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpLogonOpData::Send() in state 2
16:30:49 Command: AUTH TLS
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 502 Explicit TLS authentication not allowed
16:30:49 Trace: CFtpLogonOpData::ParseResponse() in state 2
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpLogonOpData::Send() in state 3
16:30:49 Command: AUTH SSL
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 502 Explicit TLS authentication not allowed
16:30:49 Trace: CFtpLogonOpData::ParseResponse() in state 3
16:30:49 Status: Insecure server, it does not support FTP over TLS.
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpLogonOpData::Send() in state 5
16:30:49 Command: USER test
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 331 Password required for test
16:30:49 Trace: CFtpLogonOpData::ParseResponse() in state 5
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpLogonOpData::Send() in state 5
16:30:49 Command: PASS *******
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 230 Logged on
16:30:49 Trace: CFtpLogonOpData::ParseResponse() in state 5
16:30:49 Status: Logged in
16:30:49 Trace: Measured latency of 0 ms
16:30:49 Trace: CFtpControlSocket::ResetOperation(0)
16:30:49 Trace: CControlSocket::ResetOperation(0)
16:30:49 Trace: CFileZillaEnginePrivate::ResetOperation(0)
16:30:49 Trace: CFtpControlSocket::FileTransfer()
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpFileTransferOpData::Send() in state 0
16:30:49 Status: Starting download of /en.lng
16:30:49 Trace: CFtpChangeDirOpData::Send() in state 0
16:30:49 Trace: CFtpChangeDirOpData::Send() in state 2
16:30:49 Command: CWD /
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 250 CWD successful. "/" is current directory.
16:30:49 Trace: CFtpChangeDirOpData::ParseResponse() in state 2
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpChangeDirOpData::Send() in state 3
16:30:49 Command: PWD
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 257 "/" is current directory.
16:30:49 Trace: CFtpChangeDirOpData::ParseResponse() in state 3
16:30:49 Trace: CFtpControlSocket::ResetOperation(0)
16:30:49 Trace: CControlSocket::ResetOperation(0)
16:30:49 Trace: CControlSocket::ParseSubcommandResult(0)
16:30:49 Trace: CFtpFileTransferOpData::SubcommandResult() in state 1
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpFileTransferOpData::Send() in state 5
16:30:49 Trace: CFtpRawTransferOpData::Send() in state 1
16:30:49 Command: TYPE I
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 200 Type set to I
16:30:49 Trace: CFtpRawTransferOpData::ParseResponse() in state 1
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpRawTransferOpData::Send() in state 2
16:30:49 Command: PASV
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 227 Entering Passive Mode (127,0,0,1,251,42)
16:30:49 Trace: CFtpRawTransferOpData::ParseResponse() in state 2
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpRawTransferOpData::Send() in state 4
16:30:49 Trace: Binding data connection source IP to control connection source IP 127.0.0.1
16:30:49 Command: RETR en.lng
16:30:49 Trace: CTransferSocket::OnConnect
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 150 Opening data channel for file download from server of "/en.lng"
16:30:49 Trace: CFtpRawTransferOpData::ParseResponse() in state 4
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpRawTransferOpData::Send() in state 5
16:30:49 Trace: CTransferSocket::OnReceive(), m_transferMode=2
16:30:49 Trace: CTransferSocket::TransferEnd(1)
16:30:49 Trace: CFtpControlSocket::OnReceive()
16:30:49 Response: 226 Successfully transferred "/en.lng"
16:30:49 Trace: CFtpRawTransferOpData::ParseResponse() in state 5
16:30:49 Trace: CControlSocket::SendNextCommand()
16:30:49 Trace: CFtpRawTransferOpData::Send() in state 8
16:30:49 Trace: CFtpControlSocket::TransferEnd()
16:30:49 Trace: CFtpControlSocket::ResetOperation(0)
16:30:49 Trace: CControlSocket::ResetOperation(0)
16:30:49 Trace: CControlSocket::ParseSubcommandResult(0)
16:30:49 Trace: CFtpFileTransferOpData::SubcommandResult() in state 7
16:30:49 Trace: CFtpControlSocket::ResetOperation(0)
16:30:49 Trace: CControlSocket::ResetOperation(0)
16:30:49 Status: File transfer successful, transferred 6.265 bytes in 1 second
16:30:49 Trace: CFileZillaEnginePrivate::ResetOperation(0)
16:30:49 Trace: CFileZillaEnginePrivate::ResetOperation(0)
答案 0 :(得分:0)
解决!似乎是模拟器特定的 - 它只发生在Genymotion。