How to connect to FTP over TLS/SSL (FTPS) server in Java - java

I'm stuck in connecting to FTP over TLS/SSL (FTPS) server. I am using SimpleFTP library through am able to connect FTP server without SSL but could not connect FTPS.
It is giving me this error at line 2 (ftp.connect),
SimpleFTP received an unknown response when connecting to the FTP server:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
and am using below code
SimpleFTP ftp = new SimpleFTP();
// Connect to an FTP server on port 21.
ftp.connect("xxx.xxx.xxx.xxx", 21, "username", "pwd");
//getting error at (ftp.connect) above line
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("web");
ftp.disconnect();

The SimpleFTP class/library does not support TLS/SSL at all.
Use the FTPSClient class from the Apache Commons Net library instead.
See the official example for the FTPClient class and just substitute the FTPClient with the FTPSClient.
FTPSClient ftpClient = new FTPSClient();
ftpClient.connect(host);
ftpClient.login(user, password);
The FTPSClient class defaults to an explicit TLS/SSL (recommended). In a rare case you need an implicit TLS/SSL, use new FTPSClient(true).

Related

Getting "Unable to determine system type - response: 500 Unknow command" when listing files on ESP8266 with Apache Commons Net

I try to connect to FTP server in ESP8266. Connection is successful, but I can't get list of files on the server.
My code is:
FTPClient mFtpClient = new FTPClient();
mFtpClient.setConnectTimeout(10000);
mFtpClient.connect(InetAddress.getByName(ip));
status = mFtpClient.login(userName, pass);
Log.e("isFTPConnected", String.valueOf(status));
if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
mFtpClient.enterLocalPassiveMode();
FTPFile[] mFileArray = mFtpClient.listFiles();
Log.e("Size", String.valueOf(mFileArray.length));
}
In logical I get the error :
java.io.IOException: Unable to determine system type - response: 500 Unknow command.
I use Apache Commons Net FTP library. So what is wrong in my code? From FileZilla Windows client, I can connect. May be the reason is that in ESP is SPIFF file system? Or another one reason?
Thanks for answers, and interest!
Your server does not support SYST command, that the FTPClient needs to decide how to parse a response of LIST command.
Solutions are:
If your server supports MLSD command, use mlistDir instead of listFiles.
Or use System.setProperty to set FTP_SYSTEM_TYPE_DEFAULT or FTP_SYSTEM_TYPE to suggest what directory listing format your server is using.

running local host openfire and smack client library andorid

I installed openfire local server on my laptop and using smack library as xmpp client. I followed this and
this.
After running code I dont see any exception and I don't understand if my user is connected to server. Is there any way to check connection .
Code for making connection to server
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setUsernameAndPassword(loginUser, passwordUser);
config.setServiceName(ConnectXmpp.DOMAIN);
config.setHost(ConnectXmpp.DOMAIN);
config.setPort(5222);
config.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(config.build());
Log.e("connection",""+connection);//this line to check connection
XMPPConnectionListener connectionListener = new XMPPConnectionListener();
connection.addConnectionListener(connectionListener);
I added user at server and using that username for login at app side.
please guide me where i can check incoming messages fromclient to server , im using embedded database.
Check your callback listener; it will return the connection object.

Manual FTP with commons.net.ftp

I am trying to use apache commons to send manual FTP commands as I have to send non-standard FTP commands to a specific server (that accepts them)
Before I try to send these non-standard commands I want to get FTP working manually with commons.net.ftp. Unfortunately I seem to be missing something.
This works fine (i.e. it retrieves the list of files)
FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);
ftp.connect("ftp.mozilla.org");
ftp.login("anonymous", "");
ftp.enterLocalPassiveMode();
FTPFile[] fileList = ftp.listFiles("/");
This doesn't
FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);
ftp.connect("ftp.mozilla.org");
ftp.login("anonymous", "");
ftp.sendCommand("PASV");
ftp.sendCommand("NLST");
I get the appropriate response for ftp.sendCommand("PASV"); but it times out on ftp.sendCommand("NLST"); finally giving me
425 Failed to establish connection.
I have tried to research the topic but most advice on this error is for people setting up servers (and it's usually a firewall problem).
Why does it work when net.ftp does it, but not when I send the commands manually?
Sending the PASV command manually is not enough, the client has to open the data connection to the port specified by the server in response to the PASV command. This is performed by calling the enterLocalPassiveMode() method. Since sending PASV manually doesn't initialize the data connection you get an error shortly after.
See http://slacksite.com/other/ftp.html#passive for more details on the FTP protocol.

Connecting to an FTP server via a proxy using Apache FTPClient

Using the Apache FTPClient, i can usually connect using the following statements:
FTPClient client = new FTPClient();
client.connect("ftp.myhost.com");
client.login("myUsername", "myPassword");
client.changeWorkingDirectory("/fileFeed");
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
The above works fine but now i am to connect to the FTP site, i have to use a proxy server. The instructions i got is that i should connect to the proxy server and specify the actual ftp server in the username. So to log on i would use the following details to connect:
ftp ftp.myProxyServer.com
username myUsername#ftp.myhost.com
password myPassword
I tried connecting directly using the command prompt and i can connect to the ftp.myProxyServer.com host and it does forward me to the intended ftp site if i specify myUsername#ftp.myhost.com as the host username. The problem is that the above type of connection is not accepted in Java using Apache FTPClient:
FTPClient client = new FTPClient();
client.connect("ftp.myProxyServer.com");
client.login("myUsername#ftp.myhost.com", "myPassword");
client.changeWorkingDirectory("/fileFeed");
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
Is there anything i am missing or would the above not work? I tried a direct connection and that works fine.
The FTPClient usually is in active ftp mode, and in case your proxy is not able to initiate a tcp connection back to your client computer (for firewall/DMZ reasons) then you have to switch to passive mode:
FTPClient client = new FTPClient();
client.connect("ftp.myProxyServer.com");
client.login("myUsername#ftp.myhost.com", "myPassword");
client.enterLocalPassiveMode(); //switch from active to passive
client.changeWorkingDirectory("/fileFeed");
...
(Furthermore I would like to recommend to always check the return codes of the method calls, but probably they are ommitted for sake of clarity)
Sorry for the late attempt to answer your question...

FTP upload file error code=425, message= Can't open data connection

I'm using ftp4j for FTP connection through applet to upload file.
From server, I already open FTP site with port 27, targetURL = local IP address is 192.168.x.xxx, and grant access for user name FTPUser, password xxxx.
FTPClient client = new FTPClient();
client.connect(targetURL, port);
client.setType(client.TYPE_BINARY);
client.login(user, password);
fis = new FileInputStream(targetFile);
client.upload(targetFile);
It connects and uploads file successfully through applet, within internal network
However, when I try to test with external network, router is already configured by using NAT to translate the local IP to external IP: 175.136.xxx.xxx with port 47027 (already tested the FTP Connection using command), I got this error message:
user: FTPUser, password: xxxx port 47027
network: Connecting http://175.136.xxx.xxx:47027/ with proxy=DIRECT
network: Connecting http://175.136.xxx.xxx:20459/ with proxy=DIRECT
it.sauronsoftware.ftp4j.FTPException [code=425, message= Can't open data connection.]
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2658)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2539)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2410)
As from the error log, it tried to establish the connection through port 47027, then after that change to 20459 (that I do not know where it comes from).
I can see the file is created in the server FTP site, but with 0 byte.
Anyone knows what's wrong with this situation?
Use passive mode instead. Your side might be blocking the port.

Categories