0

我正在尝试使用 Apaches FTPSClient 连接到 FTP,但我不断收到此错误:

06-15 09:15:14.749: W/System.err(29451): java.io.IOException: 无法初始化 SSL 上下文

以下代码会发生这种情况,错误出现在第二行(连接)。

ftpsClient = new FTPSClient("SSL");
ftpsClient.connect(host);
int reply = ftpsClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
    ftpsClient.disconnect();
}
ftpsClient.login(user, password + nl);
ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpsClient.enterLocalPassiveMode();

关于我可能做错了什么的任何建议?

编辑:

完整的堆栈跟踪

06-15 09:15:14.749: W/System.err(29451): java.io.IOException: Could not initialize SSL context
06-15 09:15:14.749: W/System.err(29451):    at org.apache.commons.net.ftp.FTPSClient.initSslContext(FTPSClient.java:228)
06-15 09:15:14.749: W/System.err(29451):    at org.apache.commons.net.ftp.FTPSClient.sslNegotiation(FTPSClient.java:246)
06-15 09:15:14.749: W/System.err(29451):    at org.apache.commons.net.ftp.FTPSClient._connectAction_(FTPSClient.java:197)
06-15 09:15:14.757: W/System.err(29451):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:164)
06-15 09:15:14.757: W/System.err(29451):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:184)
06-15 09:15:14.757: W/System.err(29451):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:273)
06-15 09:15:14.757: W/System.err(29451):    at com.test.FTPClient$getFileListTask.doWhat(FTPClient.java:546)
06-15 09:15:14.757: W/System.err(29451):    at com.test.FTPClient$getFileListTask.doInBackground(FTPClient.java:201)
06-15 09:15:14.757: W/System.err(29451):    at com.test.FTPClient$getFileListTask.doInBackground(FTPClient.java:1)
06-15 09:15:14.757: W/System.err(29451):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-15 09:15:14.757: W/System.err(29451):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-15 09:15:14.765: W/System.err(29451):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-15 09:15:14.765: W/System.err(29451):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-15 09:15:14.765: W/System.err(29451):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-15 09:15:14.765: W/System.err(29451):    at java.lang.Thread.run(Thread.java:856)
06-15 09:15:14.765: W/System.err(29451): Caused by: java.security.KeyManagementException: No X509KeyManager found
06-15 09:15:14.765: W/System.err(29451):    at org.apache.harmony.xnet.provider.jsse.SSLParametersImpl.<init>(SSLParametersImpl.java:116)
06-15 09:15:14.765: W/System.err(29451):    at org.apache.harmony.xnet.provider.jsse.SSLContextImpl.engineInit(SSLContextImpl.java:92)
06-15 09:15:14.765: W/System.err(29451):    at javax.net.ssl.SSLContext.init(SSLContext.java:219)
06-15 09:15:14.765: W/System.err(29451):    at org.apache.commons.net.ftp.FTPSClient.initSslContext(FTPSClient.java:226)
06-15 09:15:14.765: W/System.err(29451):    ... 14 more

EDIT2:keymanager 异常

06-15 10:45:14.886: W/System.err(32467): javax.net.ssl.SSLException: Connection closed by peer
06-15 10:45:14.886: W/System.err(32467):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.commons.net.ftp.FTPSClient.sslNegotiation(FTPSClient.java:263)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.commons.net.ftp.FTPSClient._connectAction_(FTPSClient.java:197)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:164)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:184)
06-15 10:45:14.886: W/System.err(32467):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:273)
06-15 10:45:14.886: W/System.err(32467):    at com.test.FTPClient$getFileListTask.doWhat(FTPClient.java:552)
06-15 10:45:14.886: W/System.err(32467):    at com.test.FTPClient$getFileListTask.doInBackground(FTPClient.java:203)
06-15 10:45:14.886: W/System.err(32467):    at com.test.FTPClient$getFileListTask.doInBackground(FTPClient.java:1)
06-15 10:45:14.886: W/System.err(32467):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-15 10:45:14.894: W/System.err(32467):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-15 10:45:14.894: W/System.err(32467):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-15 10:45:14.901: W/System.err(32467):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-15 10:45:14.901: W/System.err(32467):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-15 10:45:14.901: W/System.err(32467):    at java.lang.Thread.run(Thread.java:856)
06-15 10:45:14.901: W/System.err(32467): java.net.SocketException: Socket closed
06-15 10:45:14.909: W/System.err(32467):    at libcore.io.Posix.sendtoBytes(Native Method)
06-15 10:45:14.909: W/System.err(32467):    at libcore.io.Posix.sendto(Posix.java:146)
06-15 10:45:14.909: W/System.err(32467):    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
06-15 10:45:14.909: W/System.err(32467):    at libcore.io.IoBridge.sendto(IoBridge.java:463)
06-15 10:45:14.909: W/System.err(32467):    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
06-15 10:45:14.917: W/System.err(32467):    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
06-15 10:45:14.925: W/System.err(32467):    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
06-15 10:45:14.925: W/System.err(32467):    at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
06-15 10:45:14.925: W/System.err(32467):    at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
06-15 10:45:14.925: W/System.err(32467):    at java.io.BufferedWriter.flush(BufferedWriter.java:124)
06-15 10:45:14.925: W/System.err(32467):    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:477)
06-15 10:45:14.925: W/System.err(32467):    at org.apache.commons.net.ftp.FTPSClient.sendCommand(FTPSClient.java:486)
06-15 10:45:14.925: W/System.err(32467):    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:537)
06-15 10:45:14.925: W/System.err(32467):    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:586)
06-15 10:45:14.925: W/System.err(32467):    at org.apache.commons.net.ftp.FTP.syst(FTP.java:1504)
06-15 10:45:14.933: W/System.err(32467):    at org.apache.commons.net.ftp.FTPClient.getSystemType(FTPClient.java:2074)
06-15 10:45:14.933: W/System.err(32467):    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2511)
06-15 10:45:14.933: W/System.err(32467):    at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2275)
06-15 10:45:14.933: W/System.err(32467):    at com.test.FTPClient$getFileListTask.doWhat(FTPClient.java:298)
06-15 10:45:14.933: W/System.err(32467):    at com.test.FTPClient$getFileListTask.doInBackground(FTPClient.java:205)
06-15 10:45:14.933: W/System.err(32467):    at com.test.FTPClient$getFileListTask.doInBackground(FTPClient.java:1)
06-15 10:45:14.933: W/System.err(32467):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-15 10:45:14.933: W/System.err(32467):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-15 10:45:14.933: W/System.err(32467):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-15 10:45:14.933: W/System.err(32467):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-15 10:45:14.933: W/System.err(32467):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-15 10:45:14.933: W/System.err(32467):    at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

2

由于某种原因,它无法创建默认值KeyManager,这就是初始化 SSL 上下文失败的原因。尝试手动安装默认的。就像是:

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(null, null);
KeyManager km = kmf.getKeyManagers()[0];

ftpsClient = new FTPSClient("SSL");
ftpsClient.setKeyManager(km);
于 2012-06-15T08:17:47.950 回答