连接到FTPS服务器时出现以下错误。它能够建立连接,但文件传输和下载不会发生。 以下是我正在使用的代码
package com.test;
import java.io.*;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPCmd;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.commons.net.util.KeyManagerUtils;
public final class FTPSExample
{
public static final void main(String[] args) throws IOException, GeneralSecurityException
{
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
int base = 0;
boolean storeFile = false, binaryTransfer = true, error = false;
String server, username, password, remote, local;
String protocol = "TLSv1.2"; // SSL/TLS
FTPSClient ftps;
server = "hostaddress";
username = "user";
password = "password";
remote="/IN";
local="E:\\abc.txt";
ftps = new FTPSClient(protocol,true);
InputStream is = new FileInputStream("abc.jks");
String file1 = "abc.jks";
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(file1), "changeit".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
KeyManager keyManager = createKeyManagers(file1,"changeit")[0];
TrustManager trustManager = createTrustManagers(file1,"changeit")[0];
//ftps.setControlEncoding("UTF-8");
ftps.setKeyManager(keyManager);
ftps.setTrustManager(trustManager);
ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
try
{
int reply;
ftps.connect(server,990);
ftps.setBufferSize(324224434);
System.out.println("Connected to " + server + ".");
reply = ftps.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftps.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
}
catch (IOException e)
{
e.printStackTrace();
if (ftps.isConnected())
{
try
{
ftps.disconnect();
}
catch (IOException f)
{
f.printStackTrace();
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
System.exit(1);
}
__main:
try
{
ftps.setBufferSize(1000);
if (!ftps.login(username, password))
{
ftps.logout();
error = true;
break __main;
}
if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE);
ftps.enterLocalPassiveMode();
ftps.execPROT("P");
ftps.execPBSZ(0L);
ftps.pwd();
if (storeFile)
{
System.out.println("Storing File");
InputStream input;
input = new FileInputStream(local);
ftps.storeFile(remote, input);
input.close();
}
else
{
ftps.setKeepAlive(true);
ftps.setRemoteVerificationEnabled(true);
ftps.sendCommand(FTPCmd.PORT);
System.out.println("Retreiving File");
FTPFile[] files = ftps.listFiles("/IN");
System.out.println("Count :"+files.length);
OutputStream output;
output = new FileOutputStream(local);
ftps.sendCommand(FTPCmd.PORT);
ftps.retrieveFile(remote, output);
System.out.println("File Retrieved");
output.close();
}
ftps.logout();
}
catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}
finally
{
if (ftps.isConnected())
{
try
{
ftps.disconnect();
}
catch (IOException f)
{
f.printStackTrace();
}
}
}
System.exit(error ? 1 : 0);
}
private static TrustManager[] createTrustManagers(String keyStorePath, String keyStorePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
return trustManagerFactory.getTrustManagers();
}
private static KeyManager[] createKeyManagers(String keyStorePath, String keyStorePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
return keyManagerFactory.getKeyManagers();
}
}
我得到的错误是
220-FileZilla Server 0.9.59 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
Connected to host.
USER ***
331 Password required for ****
PASS ******
230 Logged on
TYPE I
200 Type set to I
PROT P
200 Protection level set to P
PBSZ 0
200 PBSZ=0
PWD
257 "/" is current directory.
PORT
501 Syntax error
Retreiving File
SYST
215 UNIX emulated by FileZilla
PASV
227 Entering Passive Mode ()
[Replacing PASV mode reply address]
LIST /IN
150 Opening data channel for directory listing of "/IN"
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:980)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:646)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:785)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3409)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3339)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3016)
at com.test.FTPSExample.main(FTPSExample.java:168)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:505)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:961)
... 9 more