有沒有辦法通過org.apache.commons.net.ftp.FTPClient
實現安全的FTP?使用org.apache.commons.net.ftp.FTPClient保護FTP
如果不是,還有什麼其他Java選項?
有沒有辦法通過org.apache.commons.net.ftp.FTPClient
實現安全的FTP?使用org.apache.commons.net.ftp.FTPClient保護FTP
如果不是,還有什麼其他Java選項?
您可以使用org.apache.commons.net.ftp。 FTPSClient而不是org.apache.commons.net.ftp。 FTPClient有安全的FTP http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html
如何努力Apache的駱駝,
Apache FTPClient目前不支持SFTP。但是,您可以使用JSch - Java Secure Channel。
Onkar Joshi更詳細地介紹了用於在Java中進行FTP,SFTP,FTPS文件傳輸的庫。
使用JSch與SFTP傳輸文件的例子如下:
...
private static final Logger logger = Logger.getLogger(YourClass.class.getName());
public boolean sendDataViaSFTP(String contents) throws Exception {
String hostname = "<FTP hostname/IP>";
String username = "<FTP username>";
String password = "<FTP password>";
String remoteDirectory = "<FTP remote directory>";
int ftpPort = 22;
logger.info("*** Creating FTP session. ***");
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp c = null;
//Now connect and SFTP to the SFTP Server
try {
//Create a session sending through our username and password
session = jsch.getSession(username, hostname, ftpPort);
logger.info("*** FTP Session created. ***");
session.setPassword(password);
//Security.addProvider(new com.sun.crypto.provider.SunJCE());
//Setup Strict HostKeyChecking to no so we dont get the
//unknown host key exception
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
logger.info("*** Session connected. ***");
//Open the SFTP channel
logger.info("*** Opening FTP Channel. ***");
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
//Change to the remote directory
logger.info("*** Changing to FTP remote dir: " + remoteDirectory + " ***");
c.cd(remoteDirectory);
//Send the file we generated
try {
String filename = "myfile.txt";
logger.info("*** Storing file as remote filename: " + filename + " ***");
ByteArrayInputStream bis = new ByteArrayInputStream(contents.getBytes());
c.put(bis, filename);
return true;
} catch (Exception e) {
logger.info("*** Storing remote file failed. " + e.toString() + " ***");
throw e;
}
} catch (Exception e) {
logger.info("*** Unable to connect to FTP server. " + e.toString() + " ***");
throw e;
} finally {
//
//Disconnect from the FTP server
//
try {
if(session != null)
session.disconnect();
if(channel != null)
channel.disconnect();
if(c != null)
c.quit();
} catch (Exception exc) {
logger.severe("*** Unable to disconnect from FTP server. " + exc.toString()+" ***");
}
logger.info("*** SFTP Process Complete. ***");
}
}
...
首先,確保你理解FTPS(安全FTP)和SFTP之間的差異:
FTPS versus SFTP versus SCP
如果您需要FTPS(安全FTP),你可以使用FTPSClient
而不是從FTPClient
阿帕奇百科全書庫:
https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html
如果需要SFTP,請參閱:
How to retrieve a file from a server via SFTP?
Java SFTP Transfer Library
你發現了什麼了嗎? – Rob
有沒有原因你不能SSH?如果不是,請嘗試Apache Mina或Jsch – amphibient
將它切換到「FTPSClient」的子類是什麼? –