Our network team only allow us to connect to our third party client thru proxy server.
Is there a way to add a proxy server to FTPS client of apache commons net?
If it is not possible, can you tell a way on how to do it.
By the way here's the code that is working outside of the company network
String server = "ftp.xxxx.com";
String username = "username";
String password = "password";
String remoteFile = "xmlSR.xml";
String localFile = "c:/downloadedfile.xml";
String protocol = "TLS"; // TLS / SSL
int timeoutInMillis = 3000;
boolean isImpicit = false;
FTPSClient client = new FTPSClient(protocol, isImpicit);
client.enterLocalActiveMode();
client.setRemoteVerificationEnabled(false);
client.setActivePortRange(50000, 50200);
client.setDataTimeout(timeoutInMillis);
client.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
try {
int reply;
client.connect(server);
client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.execPBSZ(0);
client.execPROT("P");
System.out.println("Connected to " + server + ".");
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.listFiles();
boolean retrieved = client.retrieveFile(remoteFile,
new FileOutputStream(localFile));
} catch (Exception e) {
if (client.isConnected()) {
try {
client.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
return;
} finally {
System.out.println("# client disconnected");
client.disconnect();
}
even we tried to add some system property for proxyHost and proxyPort
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyHost", "yyyy.com");
System.setProperty("ftp.proxyPort", "80");
System.setProperty("ftp.proxyHost", "yyyy.com");
System.setProperty("socksProxyPort", "80");
System.setProperty("socksProxyHost", "yyyy.com");
error message
Could not connect to server.
java.net.UnknownHostException: ftp.xxxx.com
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1202)
at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
at java.net.InetAddress.getAllByName(InetAddress.java:1083)
at java.net.InetAddress.getAllByName(InetAddress.java:1019)
at java.net.InetAddress.getByName(InetAddress.java:969)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)# client disconnected
at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.ti.itg.peit.bom.TestingApache.main(TestingApache.java:44)
Thank you very much.
Gerald
Its possible..see the link below....
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/ProxySelector.java
Well using Apache's common lib to access FTP using proxy server, please set the RemoteVerificationEnabled to false after creation of FTPClient object.
eg:
FTPClient fc = new FTPClient();
fc.setRemoteVerificationEnabled(false);
You can use java.net.Proxy class which is for Java 1.5 and above, this
is used to set or unset the Proxy per connection basis
By using the java.net.ProxySelector, will determine a Proxy for each Connection.
Related
this is my code:
String serverAddress = "ftp://ftp.nasdaqtrader.com/symboldirectory/"; // ftp server address
int port = 21; // ftp uses default port Number 21
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(serverAddress, port);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE/FTP.ASCII_FILE_TYPE);
String remoteFilePath = "/nasdaqtraded.txt";
File localfile = new File(System.getProperty("user.dir")+"\\src\\test\\resources\\stocks.txt");
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localfile));
boolean success = ftpClient.retrieveFile(remoteFilePath, outputStream);
outputStream.close();
if (success) {
System.out.println("Ftp file successfully download.");
}
} catch (IOException ex) {
System.out.println("Error occurs in downloading files from ftp Server : " + ex.getMessage());
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
And i am running it from localhost so i can download a list of stocks from nasdaq site, problem is it gives me this error:
Error occurs in downloading files from ftp Server : ftp://ftp.nasdaqtrader.com/symboldirectory/: invalid IPv6 address
I understand that is because i am trying to download the file from localhost, is there any way around it?
I am just trying to download this file:
ftp://ftp.nasdaqtrader.com/symboldirectory/nasdaqtraded.txt
to my computer, that's it.
The FTPClient class's connect() method expects to be passed the hostname of the server to connect to.
As with all classes derived from SocketClient, you must first connect to the server with connect before doing anything, and finally disconnect after you're completely finished interacting with the server.
However, your code is passing in a URI, which is being misinterpreted as an IPv6 address (probaby because it contains a colon).
You should instead connect() to the hostname of the server.
String hostname = "ftp.nasdaqtrader.com";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(hostname, port);
I had same issue and found this helpful
Read a file from NASDAQ FTP Server
Maven Dependencies:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
Java Code:
FTPClient ftpClient = new FTPClient();
ftpClient.setStrictReplyParsing(false);
int portNumber = 21;
String pass = "anonymous";
try {
// connect to NASDAQ FTP
ftpClient.connect("ftp.nasdaqtrader.com", portNumber);
ftpClient.login(pass, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
if (ftpClient.isConnected()) {
log.debug("connection successful");
ftpClient.changeWorkingDirectory("/SymbolDirectory");
String remoteFile = "nasdaqlisted.txt";
InputStream in = new BufferedInputStream(ftpClient.retrieveFileStream(remoteFile));
String text = IOUtils.toString(in, StandardCharsets.UTF_8.name());
if(text != null) {
log.debug("write successful; \n {}", text);
}
}
ftpClient.logout();
} catch (IOException e) {
log.error("connection failed", e);
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
log.error("failed to disconnect", e);
}
}
I use this code to connect to ftps with explicit encryption
String host = "10.21.13.66";
int port = 21;
String username = "user";
String password = "pass";
try {
FTPSClient ftpClient = new FTPSClient("ssl",false);
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Login
(ftpClient.login(username, password))
}
} catch (IOException ioe) {
System.out.println("FTP client received network error");
}
when I try to connect to my own ftps it returns this error: 431 No security resource for TLS/SSL encryption, probably there is no selected SSL certificate
any help would be appreciated;
Try setting the auth on ftps client using below code after object is constructed:
ftpClient.setAuthValue("TLS");
I want to check the online/offline status about a Database Server with Java.
Can I check this with a Socket connection over the port? I want to do this wihtout a Database connection with jdbc because the login and Database system info is unknown.
You can try the following:
try {
Socket socket = new Socket("127.0.0.1", port); //Port dependent on your DB/Server
// Server is up
socket.close();
}
catch (IOException e)
{
// Server is down
}
Yes, you can just open a Socket to the address and port of your databse server, if you get an IOException the server is down. (tested with postgress)
public boolean isDatabaseOnline(String address, int port) {
boolean result;
try {
Socket socket = new Socket(address, port);
socket.close();
result = true;
} catch (IOException e) {
result = false;
}
return result;
}
The above approaches don't really consider timing out in case the remote is unreachable, and a reasonable timeout should be defined because the default value is 20 seconds!!
You can state a timeout using the socket.connect method AFTER you create a blank socket.
SocketFactory sf = SocketFactory.getDefault();
try (Socket socket = sf.createSocket()) {
socket.connect(new InetSocketAddress(ipAdder, port), timeoutInMillis);
logger.info("database is up");
} catch (IOException e) {
logger.info("database is down");
}
The example above uses try with resources
i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3
i have followed this article for create my connection, and with official examples i've controlled article.
my java code here:
private void connect(String host, String user, String pwd) {
try{
ftp = new FTPSClient(false);
//ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host,22);//error is here
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}catch(Exception ex){
ex.printStackTrace();
}
}
I do not understand where I went wrong.
The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library
JSch jsch = new JSch();
Session session = jsch.getSession( user, host, port );
session.setConfig( "PreferredAuthentications", "password" );
session.setPassword( pass );
session.connect( FTP_TIMEOUT );
Channel channel = session.openChannel( "sftp" );
ChannelSftp sftp = ( ChannelSftp ) channel;
sftp.connect( FTP_TIMEOUT );
SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {
public static void main(String[] args) throws Exception {
String user = "username";
String host = "hostadress";
int port = 22;
String pass = "password";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
}
try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.
Change the port to 21 to connect to FTP server instead of SFTP(port 22)
I am using TCP/IP sockets to create a client and server applicaton. Originally I was using regular sockets but now I have decided to use SSL for my connection. I have created a keystore and have tried running my application but it has yet to be successful.
Here is my code for the server
public class ArFileServer {
public static void main(String[] args)
{
boolean listening = true;
ServerSocketFactory serversocketfactory;
ServerSocket serverSocket;
try
{
//serverSocket = new ServerSocket(4445);
serversocketfactory = SSLServerSocketFactory.getDefault();
serverSocket = serversocketfactory.createServerSocket(4445);
String keystore = System.getProperty("javax.net.ssl.trustStore");
System.out.println(keystore);
// infinite loop to continually listen for connection requests made by clients
while (listening)
{
new ClientConnection(serverSocket.accept()).start();
if (serverSocket != null)
{
System.out.println("Connection to client established");
}
}
serverSocket.close();
}
catch (IOException e)
{
System.out.println("Error could not create socket connection to port, check that port is not busy");
}
}
}
and here is the client code:
public class ClientSocket
{
SocketFactory socketfactory = null;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
// establish a connection to All Care's server application through socket 4444 (adjust localhost to reflect the IP address that the server
// is being run from)
public ClientSocket()
{
try
{
//clientSocket = new Socket("localhost", 4445);
//SocketFactory socketfactory = SSLSocketFactory.getDefault();
clientSocket = socketfactory.createSocket("192.168.1.8", 4445);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String truststore = System.getProperty("javax.net.ssl.trustStore");
System.out.println(truststore);
}
catch (IOException e)
{
System.out.println("Could not connect to All Care Server Application : " + e.getMessage());
}
}
}
I am also using these runtime arguments:
-Djavax.net.ssl.keyStore=C:\Users\Chris\Documents\NetBeansProjects\ArFile\keystore.jks -Djavax.net.ssl.keyStorePassword=password
When I try to print out the truststore it always returns null, what am I doing wrong?
When I try to print out the truststore it always returns null
Because you never set it. All you are doing is printing out the value of a system property. If you didn't set it, it is null.
what am I doing wrong?
Nothing yet, except printing out meaningless information. But much of your code doesn't make sense:
if (serverSocket != null)
{
System.out.println("Connection to client established");
}
serverSocket being non-null (a) is inevitable at this point, and (b) doesn't have anything do with the client socket being established, which is inevitable at this point.
catch (IOException e)
{
System.out.println("Error could not create socket connection to port, check that port is not busy");
}
An IOException at this point could mean many things, but the one thing it doesn't mean is 'cannot create socket connection to port'. It is the client that does the connecting: the server accepts connections. When you catch an exception, always print its message, don't just make up your own.
You need to define both trustStore and keyStore in runtime arguments:
-Djavax.net.ssl.keyStore=xxx.ks
-Djavax.net.ssl.keyStorePassword=yyy
-Djavax.net.ssl.trustStore=xxx.ks
-Djavax.net.ssl.trustStorePassword=yyy
Both can be same file.
trustStore contains public keys of others.
keyStore contains own keys and certificates.