ftp client (from apache.org) and socket except - java

I use the library from apache.org and use the code from java2s.com:
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileOutputStream fos = null;
client.connect("ftp.domain.com");
client.login("admin", "secret");
client.enterLocalPassiveMode();
String filename = "sitemap.xml";
fos = new FileOutputStream(filename);
client.retrieveFile("/" + filename, fos);
fos.close();
client.disconnect();
}
}
I downloaded the library, moved it to the lib folder and renamed it to cn.jar.
compiling: (under Windows 7)
javac -cp ".;lib\cn.jar" Main.java
running: (under Windows 7)
java -cp ".;lib\cn.jar" Main
and I've got: http://freelifer.narod.ru/some.png
How to fix it? What is wrong?

My guess: the FTP protocol defines two connections - a data connection and a control connection. Your sample code established the control connection successfully:
client.connect("ftp.domain.com");
client.login("admin", "secret");
The default constructor of FTPClient defines that the data connection should be established in active mode (meaning that the server will try to open a connection to your client when you request a file). I think that the data connection cannot be opened due to your firewall or some other network issue. The data connection is opened here:
client.retrieveFile("/" + filename, fos);
You may try passive mode or you may check your network settings again. Passive mode is entered by calling the enterLocalPassiveMode method of FTPClient. This method causes a PASV (or EPSV) command to be issued to the server. Example:
client.connect("ftp.domain.com");
client.login("admin", "secret");
client.enterLocalPassiveMode();
Cheers!

Related

Can you connect to AS400 through Apache TelnetClient in Java?

I am trying to connect to AS400 using the TelnetClient from Apache in Java.
The goal for now is to simply connect and read the screen line by line and display each line. I will focus on input and interaction later.
When I connect via telnet, my program gets 'hung up' when it tries to read the input line. I think the thread is blocked by: line = bfIn.readLine() because this is where the application stops. It does not crash, it is just stuck there.
If I connect to a simple telnet switch using a different host and the same program, the program prints out the page just fine, so I am wondering if there is an extra step to connect to the AS400 specifically? Can you connect to AS400 through Apache TelnetClient for java? If not, how can you connect to AS400 through java and interact with the page (seeing what the page displays)?
Here is the code I have been working with:
import org.apache.commons.net.telnet.TelnetClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "%";
public AutomatedTelnetClient(String server, String user, String password) {
try {
// Connect to the specified server
telnet.connect(host, 23);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
BufferedReader bfIn = new BufferedReader(new InputStreamReader(in));
PrintWriter writer = new PrintWriter(telnet.getOutputStream(), true);
System.out.println("BufferedReader ready to be read: " + bfIn.ready());
try {
String line;
while ((line = bfIn.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException e) {
System.out.println("Exception");
e.printStackTrace();
}
telnet.disconnect();
in.close();
bfIn.close();
An issue which I think might be occurring is the BufferedReader may not be able to pick up what AS400 returns to it upon connection so it cannot read it correctly.
Please note that the bufferedReader.ready() is returning false. I have made sure the host is valid and working using a command line connection.
Standard telnet can work with the IBM i, but it's not ideal since the 5250 protocol is designed for "screen at a time" "smart" terminals, not line or character at a time dumb terminals.
Are you aware of the TN5250J project?
tn5250j is a 5250 terminal emulator for the AS/400 written in Java.
Also for mobile access, something else to look at would be IBM i Mobile Access provided by IBM.

FTPSClient, What values to give for Remote and Local

I am new to FTPSClient i trying to connect to a FTPS created in my laptop. i don't exactly what some of the methods working and their parameter meaning.
For example,
In my code i have created a FTPSClient as below:
FTPSClient ftps =new FTPSClient();
Then connected to a server use connect() method with ip address.
ftps.connect("172.xx.xx.xxx");
After every step i will check the reply code using.
ftps.getReplyCode();
In the below code i know that
username = system username
password = the password to login
ftps.login(username, password);
In the my system in Internet Information Service(IIS). Created an ftp server with ssl and given the below directory to share.
C:\Users\karan-pt2843\Desktop\FTPS
Want to send the file in below directory to the server.
D:\sam.txt
Now i want to store a file in the server in the given above directory and i tried using
remote="";
local="";
InputStream input;
input = new FileInputStream(local);
ftps.storeFile(remote, input);
input.close();
I don't know what value to give for remote and local. please help me with the values to give on them and the what happens internal.
// Use passive mode as default because most of us are
// behind firewalls these days.
ftps.enterLocalPassiveMode();
...
String remote = "samFromClient.txt"; //Place on FTP
String input = "D:/sam.txt" //Place on your Client
//Your FTP reads from the inputstream and store the file on remote-path
InputStream input = new InputStream(new FileInputStream(input));
ftps.storeFile(remote, input);
input.close();
ftps.logout();
...
Taken from: Apache example

Java code for connect alfresco through ftps connection

When I try to connect our alfresco through SFTP it is not able to connect alfresco. It hangs the explorer and no error goes in the logger file also.
public void FTPTest()throws SocketException, IOException, NoSuchAlgorithmException
{
FTPSClient ftp = new FTPSClient("SSL");
System.out.println("1");
ftp.connect("172.17.178.144",2121); // or "localhost" in your case
System.out.println("2"+ftp.getReplyString());
System.out.println("login: "+ftp.login("admin", "admin"));
System.out.println("3"+ ftp.getReplyString());
ftp.changeWorkingDirectory("/alfresco");
// list the files of the current directory
FTPFile[] files = ftp.listFiles();
System.out.println("Listed "+files.length+" files.");
for(FTPFile file : files) {
System.out.println(file.getName());
}
// lets pretend there is a JPEG image in the present folder that we want to copy to the desktop (on a windows machine)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // don't forget to change to binary mode! or you will have a scrambled image!
FileOutputStream br = new FileOutputStream("C:\\Documents and Settings\\casonkl\\Desktop\\my_downloaded_image_new_name.jpg");
ftp.retrieveFile("name_of_image_on_server.jpg", br);
ftp.disconnect();
}
I got output in our console only
1
at the execution of ftp.connect("172.17.178.144",2121); this code system will be hang no error got in our console
I am able to connect to my Alfresco through SFTP with the Filezila FTP client software. Can any one help me resolve this issue?
If I'm not mistaken then Alfresco chose for FTPS.
So try it with the following code here: http://alvinalexander.com/java/jwarehouse/commons-net-2.2/src/main/java/examples/ftp/FTPSExample.java.shtml

FTP error while downloading/uploading with ftp4j library in java application

I want to download a file in java application and when I try it, it creates the file on my hard drive but then fails to download it completely. I am using the ftp4j library to do it.
import it.sauronsoftware.ftp4j.*;
public class Main {
public static void main (String args[]){
FTPClient client = new FTPClient();
try{
client.connect("ftp.myaddress.comlu.com");
client.login("username", "password");
System.out.println("Connection created");
client.download("public_html/ZScreen.png", new java.io.File("d:/xxx/ZScreen.png"));
System.out.println("Download successful");
client.disconnect(true);
}
catch (Exception FTPException){
System.out.println("Shit hit the fan");
}
}
}
I always get the Connection created and Shit hit the fan. Also, there is a file created on my hard drive but it's size is 0 bytes.
This is the stack race
Connection created
java.net.SocketException: Connection reset
Shit hit the fan
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openPassiveDataTransferChannel(FTPClient.java:3538)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3473)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3302)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3213)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3078)
at Main.main(Main.java:9)
Apparently there is a bug on the Windows 7 firewall related to using FTP on IPv6 that would explain your problem. See bug report here.
Any one of the following workarounds should suffice to fix it:
Run the following as an administrator in a Windows console:
netsh advfirewall set global StatefulFtp disable
Run the JVM with the option: -Djava.net.preferIPv4Stack=true
You do not have the rights to write on the folder. Check if the repertory is not under "read-only" state.

FTPClient Uploading File = SocketException: Connection reset

I'm trying to upload a simple txt file via FTP using XAMPP and FileZilla.
I'm using the Apache Commons Net 3.0.1 Library.
This is my code, very basic things:
FTPClient client = new FTPClient();
InputStream in = new ByteArrayInputStream("IT WORKS! :D".getBytes());
try {
client.connect("localhost");
client.login("user", "password");
client.enterLocalPassiveMode();
client.storeFile("textfile.txt", in);
} finally {
try {
in.close();
client.logout();
client.disconnect();
} catch (Exception e) {
}
}
But... storeFile() throws a java.net.SocketException:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.read(BufferedReader.java:175)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:310)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:474)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:596)
at org.apache.commons.net.ftp.FTP.pasv(FTP.java:945)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:719)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:551)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1704)
at ftpexample.ftpexample.main(ftpprova.java:17)
What's the problem?? :(
I tried also on an online hosting service, with the same result...
I wonder if this is a firewall or windows' services related problem??
Solved by running this as administrator in the command prompt:
netsh advfirewall set global StatefulFTP disable
This is a Java 7 bug on Windows machines, it is reported here.
Set:
client.setUseEPSVwithIPv4( true );
This works if you can't make changes to Window's firewall settings.
I'm honestly not sure but you should try the following:
Use something like the following code:
System.out.println(client.getReplyCode());
for(String s : client.getReplyStrings())
System.out.println(s);
after client.login("user", "password"); to verify the status of your connection.
If you don't get any good hints from the code above, after invoking client.storeFile("textfile.txt", in); try to add client.completePendingCommand();.
Good luck! :)

Categories

Resources