Correct way to download ftp file from localhost - java

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);
}
}

Related

fetch data from ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/ using java

how can I fetch movie.list.gz folder from the link
1: ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/, I have used a code to access the FTP server for fetching data but it asks for port no. which I don't know. Can anyone please tell how to fetch data from the above link in java. I am developing a Web Application which will display all the movie names present in IMDB database.
CODE:
String server = "ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/pub/movie.list.gz";
File downloadFile1 = new File("F:/softwares/mov.list.gz");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

How to copy a file with FTP Client using regular expression in Java

String server = "www.test.com";
int port = 21;
String username = "test";
String password = "test";
FTPClient ftpclient = new FTPClient();
try {
ftpclient.connect(server,port);
ftpclient.login(username, password);
ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile = "/home/test/workspace/9001_20150918165942_00085.xml";
File downloadfile = new File("C:/Users/Workspace/9001_20150918165942_00085.xml");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadfile));
boolean success = ftpclient.retrieveFile(remoteFile, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
}
finally {
try {
if (ftpclient.isConnected()) {
ftpclient.logout();
ftpclient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
I am using the above FTPClient to download a file from a Linux machine to Windows machine. For the intended file it's working fine.
But I am trying to achieve the same using regular expression. For instance, /home/test/workspace/*.xml which will be downloaded to C:/Users/Workspace/*.
Use the FTPClient.mlistDir method (or FTPClient.listFiles) to list files in the remote directory
Iterate the list to find files matching your pattern.
Download the matched files, one by one.

SFTP connection throws odd FileTransferException that exposes SFTP password

It is really bizarre, why would zehon return my location of my eclipse, and my SFTP password as part of a FileSystemException?
I have checked that the remote host is indeed a SFTP server, and the client is connecting using SFTP.
Zehon API here
Stacktrace
Reading file from C:\srcFolde\FileToBeUploaded.zip
com.zehon.exception.FileTransferException:
org.apache.commons.vfs.FileSystemException:
Unknown message with code:
"C:<location of eclipse>/<sftp password>?" does not exist
at int result = sftp.sendFile(filePath, ftpDestFolder);
Code
SFTPClient sftp = new SFTPClient(ftpServer, 22, ftpUserName, ftpPassword, true);
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
String filePath=fileName.substring(0, fileName.length()-4) + ".zip";
String ftpDestFolder="\\sftpDestFolder";
int result = sftp.sendFile(filePath, ftpDestFolder);
Logger.debug("sftp result = " + result);
} catch (FileTransferException e) {
e.printStackTrace();
return false;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
You've used the wrong constructor. From the Javadocs
http://www.zehon.com/javadocs/com/zehon/sftp/SFTPClient.html
You've passed ftpPassword where it's expecting privateKeyPath.

FTPS client behind a proxy server using apache commons net

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.

Android How to connect to FTP?

i am trying to connect to a server through a apache FTP:
public boolean ftpConnect(String host, String user, String pass){
try {
ftpClient = new FTPClient();
ftpClient.connect(host);
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
boolean status = ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
return status;
}
} catch (SocketException e) {
Log.d("FTP", "Error: could not connect to socket " + host );
} catch (IOException e) {
Log.d("FTP", "Error: could not connect to host " + host );
}
return false;
}
If i am connected to internet through WI-FI, the above code is working, but if i am connected through 3G is not working. I've already added the permission for Internet on the
manifest. I haven't found an explanation for this on google.
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(InetAddress.getByName(Your host Url));
ftpClient.login(loginName, password);
System.out.println("status :: " + ftpClient.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
Also, see my answer here: Track FTP upload data in android?.

Categories

Resources