I am using the Apache Commons FTP library in my android application
I am making the connection through FTPS, and although it connects perfectly to the server, I have
You can try the following code, I hope it will work for your case too.
The code uses Apache Commons vsf2 for uploading file over secure ftp connection (SFTP)
try {
String filepath = "";
String serverAddress = "";
String userId = "";
String password = "";
String remoteDirectory = "";
String keyPath = "";
String passPhrase = "";
File file = new File(filepath);
if (!file.exists())
throw new RuntimeException("Error. File not found");
//Initializes the file manager
StandardFileSystemManager manager = new StandardFileSystemManager();
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
// Create remote file object
FileObject remoteFile = manager.resolveFile(createConnectionString(serverAddress, userId, password, keyPath, passPhrase, fileToFTP), createDefaultOptions(keyPath, passPhrase));
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload successful");
}
catch (Exception ex) {
ex.printStackTrace();
return false;
}
finally {
manager.close();
}
You can check more at Apache Commons VFS Documentation
After understanding the logic behind FTPS and the post by @riyaz-ali and referring to link in your comment to this article
There is a problem with Apache FTP client, it does not support TLS session resumption. You can patch the existing implementation of Apache Commons Library.
You can try the following code steps to get it working:
Add the following patched class to in your project. (This class extends the existing FTPS implementation given in Apache commons with patch)
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Locale;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import org.apache.commons.net.ftp.FTPSClient;
import com.google.common.base.Throwables;
public class PatchedFTPSClient extends FTPSClient {
@Override
protected void _prepareDataSocket_(final Socket socket) throws IOException {
if(socket instanceof SSLSocket) {
final SSLSession session = ((SSLSocket) _socket_).getSession();
final SSLSessionContext context = session.getSessionContext();
try {
final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cache = sessionHostPortCache.get(context);
final Method method = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
method.setAccessible(true);
final String key = String.format("%s:%s", socket.getInetAddress().getHostName(),
String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT);
method.invoke(cache, key, session);
} catch(Exception e) {
throw Throwables.propagate(e);
}
}
}
}
Use this modified code snippet.
TransferImagenesFTP.ftpClient = new PatchedFTPSClient();
TransferImagenesFTP.ftpClient.connect(InetAddress.getByName"), 26);
TransferImagenesFTP.ftpClient.login("", "");
TransferImagenesFTP.ftpClient.execPBSZ(0);
TransferImagenesFTP.ftpClient.execPROT("P");
TransferImagenesFTP.ftpClient.enterLocalPassiveMode();
//Now use the FTP client to upload the file as usual.
Hope this will work for you and will solve your problem.