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?.
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);
}
}
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();
}
}
}
I'm having problems when creating a socket in android. I'm using LibGDX.
#Override
public void create() {
System.out.println("Enter IP adress.");
ip = "78.61.65.198";
System.out.println("Connecting...");
socket = connection(PORT, ip);
System.out.println("Setting up InputStream");
reader = reader(socket);
System.out.println("Setting up OutputStream");
output = output(socket);
while (socket.isConnected()) {
output.println("0;" + Gdx.input.getAccelerometerX() + ";" + Gdx.input.getAccelerometerY());
}
}
public static Socket connection(int port, String ip) {
try {
return new Socket(InetAddress.getByName(ip), port);
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("Unknown host...");
return null;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to connect...");
return null;
}
}
public static BufferedReader reader(Socket socket) {
try {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
System.err.println("Couldn't get inputStream...");
e.printStackTrace();
return null;
}
}
public static PrintStream output(Socket socket) {
try {
return new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't get outputStream");
return null;
}
}
Server:
#Override
public void create() {
System.out.println("Creating server...");
server = hostServer(PORT);
System.out.println("Waiting for Client 1...");
client1 = waitForConnections(server);
System.out.println("Setting up input/output for client 1...");
client1Input = reader(client1);
client1Output = output(client1);
setScreen(new gameScreen(this));
}
public static ServerSocket hostServer(int port){
try {
return new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to host server!");
return null;
}
}
public static BufferedReader reader(Socket socket){
try {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to get input stream!");
return null;
}
}
public static PrintStream output(Socket socket){
try {
return new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to create output stream!");
return null;
}
}
public static Socket waitForConnections(ServerSocket server){
try {
return server.accept();
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to accept connection!");
return null;
}
}
It works when i do it with a desktop project, but when i do it with an android project, it doesn't work.
ERROR:
Let me know if anyone has similiar problems or have found a solution.
Assuming both your PC and your Android device are connected to the same LAN (connected to the same router), you should try to use the server's internal IP address (usually looks like: 10.0.0.X or 192.168.X.X), which you can find by running the ipconfig command on the command line in a Windows PC, or ifconfig on Linux/MAC. If your PC and Android device are connected to different networks then you should use the server's external IP address like you did in your example. If it is the latter you should also forward the port your using (1234) to your PC in your router.
When the server is startet on your pc, you cannot connect from another device than your pc as long as you try to connect to localhost/127.0.0.1. Only when running the app on your pc as desktop application the connection will work since client and server are on the same host. For other clients you need to connect to the local LAN IP address of your PC. When you try to connect to localhost/127.0.0.1 from your phone it is looking for a server running on the phone and this is not the case.
Is the client network connection estabished in an UI thread/activity? If so, network connection should established in a separate thread, eg. AsynTask for short operations and services for longer streaming operations. http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask
I'm trying to connect into FTP server, the problem is that wehn I try to log into the server with specific user it returns false.
This is the link for the ftp website: https://url.publishedprices.co.il/
The user name is: DorAlon
The password should be empty
When I'm connecting through the website it's working, it works too when i'm connecting from FileZilla, but when I do so from Java it returns false for this user. There are several more users that logged in successfully to this server, and only this user doesn't work.
Here's my code:
String server = "url.retail.publishedprices.co.il";
int port = 21;
String pass = " ";
FtpClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
boolean a = ftpClient.login("xxx", "");
boolean b = ftpClient.login("yyy", "");
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
// Checking whether the client is connected - in that case we'll logout and disconnect from the server
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
In the end the boolean a holds false, and b holds true.
It's mean the connection with the server is okay and also the login, but for the specific user it doesn't work.
How could this be possible?
I would like for some help, Thanks.
Im new to android programming. Im trying to build a basic app whice tries to connect to an ftp server. I have no problems connecting to the server in other ways. I hope you guys could tell me what I'm doing wrong.
This is my code:
public boolean ftpConnect(String host, String username,
String password, int port) {
try {
client = new FTPClient();
client.connect(InetAddress.getByName(host), 21);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
boolean status = client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
return status;
}
} catch (Exception e) {
Log.d(TAG, "Error: could not connect to host " + host);
}
return false;
}
the function is called from my main activity OnCreate() method.
When I run I just recieve the "could not connect to host" tag.
Thanks in advance!