I am trying to use apache commons to send manual FTP commands as I have to send non-standard FTP commands to a specific server (that accepts them)
Before I try to send these non-standard commands I want to get FTP working manually with commons.net.ftp. Unfortunately I seem to be missing something.
This works fine (i.e. it retrieves the list of files)
FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);
ftp.connect("ftp.mozilla.org");
ftp.login("anonymous", "");
ftp.enterLocalPassiveMode();
FTPFile[] fileList = ftp.listFiles("/");
This doesn't
FTPClient ftp = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
ftp.configure(config);
ftp.connect("ftp.mozilla.org");
ftp.login("anonymous", "");
ftp.sendCommand("PASV");
ftp.sendCommand("NLST");
I get the appropriate response for ftp.sendCommand("PASV"); but it times out on ftp.sendCommand("NLST"); finally giving me
425 Failed to establish connection.
I have tried to research the topic but most advice on this error is for people setting up servers (and it's usually a firewall problem).
Why does it work when net.ftp does it, but not when I send the commands manually?
Sending the PASV command manually is not enough, the client has to open the data connection to the port specified by the server in response to the PASV command. This is performed by calling the enterLocalPassiveMode() method. Since sending PASV manually doesn't initialize the data connection you get an error shortly after.
See http://slacksite.com/other/ftp.html#passive for more details on the FTP protocol.
Related
When trying to upload file to FTP with java program:
public void upload(String localFile,String remoteFile) throws Exception{
ftp = new FTPClient();
ftp.setControlKeepAliveTimeout(300);
ftp.connect(host,21);
ftp.enterLocalPassiveMode();
ftp.setUseEPSVwithIPv4(false);
ftp.login(user,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FileInputStream in = null;
in = new FileInputStream(localFile);
ftp.storeFile(remoteFile,in);
in.close();
ftp.disconnect();
}
I'm getting:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:243)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:230)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:539)
at org.apache.commons.net.SocketClient._connect(SocketClient.java:243)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:202)
When I try to upload the same file with command line (from linux), I'm able to do it only when using EPSV:
llnx:~ ftp anonymous#9.20.1.116
Connected to 9.20.1.116.
220 Microsoft FTP Service
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 User logged in.
Remote system type is Windows_NT.
ftp> epsv
EPSV/EPRT on IPv4 off.
ftp> put /tmp/file1.xml /dir_1/file1.xml
local: /tmp/file1.xml remote: /dir_1/file1.xml
227 Entering Passive Mode (10,40,1,149,233,168).
125 Data connection already open; Transfer starting.
100% |*************************************| 117 KB 28.66 MB/s --:-- ETA
226 Transfer complete.
120032 bytes sent in 00:00 (7.96 MB/s)
So, Why does my java code getting Connection refused?
Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?
*** I think the answer is how to run the EPSV command from Java program.
Thank you all.
Eithan.
This is purely a guess but java.net.ConnectException: Connection refused normally happens when there is nothing listening on the target host/port. You don't specify a port in the CLI example so maybe that is the problem. Try changing ftp.connect(host,21); to ftp.connect(host); to use the default. Also confirm the the hostnames are exactly the same.
This assumes that the error is on the call to connect(). You haven't provided a big enough stack trace to indicate either way.
Connection refused means that your TCP connection request has reached the remote server (or more correctly >>a<< remote server) but the server is not expecting / listening for an incoming connection. So it "refuses" it.
Here are the things to check:
Check that you have the correct remote hostname or IP address for the FTP server.
Check that you are using the correct port for the FTP server. Port 21 is the default, but it is possible that the server is on a non-standard port.
Check that the FTP server is actually running.
It is also possible that the problem is due to a firewall doing something deliberately confusing. But that is unlikely for a publicly routable FTP server.
Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?
That can't be the problem. The stacktrace shows that your application failed while trying to establish the initial connection to the server. You haven't gotten to the point where the you can make those calls.
I try to connect to FTP server in ESP8266. Connection is successful, but I can't get list of files on the server.
My code is:
FTPClient mFtpClient = new FTPClient();
mFtpClient.setConnectTimeout(10000);
mFtpClient.connect(InetAddress.getByName(ip));
status = mFtpClient.login(userName, pass);
Log.e("isFTPConnected", String.valueOf(status));
if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
mFtpClient.enterLocalPassiveMode();
FTPFile[] mFileArray = mFtpClient.listFiles();
Log.e("Size", String.valueOf(mFileArray.length));
}
In logical I get the error :
java.io.IOException: Unable to determine system type - response: 500 Unknow command.
I use Apache Commons Net FTP library. So what is wrong in my code? From FileZilla Windows client, I can connect. May be the reason is that in ESP is SPIFF file system? Or another one reason?
Thanks for answers, and interest!
Your server does not support SYST command, that the FTPClient needs to decide how to parse a response of LIST command.
Solutions are:
If your server supports MLSD command, use mlistDir instead of listFiles.
Or use System.setProperty to set FTP_SYSTEM_TYPE_DEFAULT or FTP_SYSTEM_TYPE to suggest what directory listing format your server is using.
I am attempting to use the JSch class (Java Secure Channel; jsch-0.1.50.jar) to connect to an SFTP server and send a file from within a ColdFusion (9.0.2) application (which runs atop Java 1.7.0_15). The basic code in question is:
jsch = classLoader.create("com.jcraft.jsch.JSch").init(); // ColdFusion-specific to load the jar
jschSession = jsch.getSession("myusername", "ftp.example.com", 22);
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.setTimeout(60000);
jschSession.setPassword("mypassword");
jschSession.connect();
Upon connection to a Serv-U SFTP server it is giving me the following error on the Serv-U side immediately after the connection opens:
SSH Protocol Error: packet size exceeds maximum allowed.
Serv-U then closes the session, at which point JSch throws the exception:
Session.connect: java.io.IOException: End of IO Stream Read
I am new to the JSch class, and it's possible I'm missing something obvious, but I am at a loss as to where the error may lie. Connecting to the same SFTP server from the same origin with WinSCP gives no errors. Any tips on what the code is doing wrong or where to turn next for troubleshooting?
SSH Protocol Error: packet size exceeds maximum allowed
This means that the local client received some data from the remote server which wasn't properly formatted as an SFTP protocol message. The usual reason is that the server sent some kind of plain text message through the SSH connection. There are few things that might be going on:
Your .bashrc, .bash_profile, or similar shell configuration file on the server is set to print some message.
The server is poorly configured, and it's sending some kind of greeting.
The server is sending some kind of error message.
If you have access to the ssh command-line utility, you can use that to see what the server is sending. Run something like this:
$ ssh myusername#ftp.example.com -s sftp
This will open a plain SSH session to the remote server and request the SFTP subsystem, which is the same thing an SFTP client would do. If the server starts SFTP properly, you won't see any output from this command--it'll just wait until you kill it. If you see any text from the remote server, that is the problem. You'll need to figure out why the server is sending that text and prevent it.
Using the Apache FTPClient, i can usually connect using the following statements:
FTPClient client = new FTPClient();
client.connect("ftp.myhost.com");
client.login("myUsername", "myPassword");
client.changeWorkingDirectory("/fileFeed");
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
The above works fine but now i am to connect to the FTP site, i have to use a proxy server. The instructions i got is that i should connect to the proxy server and specify the actual ftp server in the username. So to log on i would use the following details to connect:
ftp ftp.myProxyServer.com
username myUsername#ftp.myhost.com
password myPassword
I tried connecting directly using the command prompt and i can connect to the ftp.myProxyServer.com host and it does forward me to the intended ftp site if i specify myUsername#ftp.myhost.com as the host username. The problem is that the above type of connection is not accepted in Java using Apache FTPClient:
FTPClient client = new FTPClient();
client.connect("ftp.myProxyServer.com");
client.login("myUsername#ftp.myhost.com", "myPassword");
client.changeWorkingDirectory("/fileFeed");
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setFileTransferMode(FTPClient.BLOCK_TRANSFER_MODE);
Is there anything i am missing or would the above not work? I tried a direct connection and that works fine.
The FTPClient usually is in active ftp mode, and in case your proxy is not able to initiate a tcp connection back to your client computer (for firewall/DMZ reasons) then you have to switch to passive mode:
FTPClient client = new FTPClient();
client.connect("ftp.myProxyServer.com");
client.login("myUsername#ftp.myhost.com", "myPassword");
client.enterLocalPassiveMode(); //switch from active to passive
client.changeWorkingDirectory("/fileFeed");
...
(Furthermore I would like to recommend to always check the return codes of the method calls, but probably they are ommitted for sake of clarity)
Sorry for the late attempt to answer your question...
I installed the WordnetSimilarity server on my Ubuntu.
I launch it with the command:
$ similarity_server.pl --logfile server.log
Now I want to use it from my Java/Groovy app, and I wrote this code to get the server version:
// open connection
Socket kkSocket = new Socket("localhost", 31134);
def out = new PrintWriter(kkSocket.getOutputStream(), true);
def inbuf = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
out.write("v") // command for the server
out.write("\015\012") // end of message
out.flush()
// so far so good
println inbuf.readLine()
// no response, hanging...
Nothing happens, and the server log is empty. The server protocol is defined in here: http://search.cpan.org/~tpederse/WordNet-Similarity-2.05/utils/similarity_server.pl
The server is definitely running, because if I stop it, the socket allocation fails.
Any hint?
Cheers, Mulone
It looks like it should work. I suspect that there is a problem with the server. Some things to try:
Are there any other clients you can use to see if the server is responding properly?
Is there any "verbose" output option for the server?
Is the server able to access whatever resources it needs, e.g., WordNet over an internet connection?
Can you debug the server process?
On the client, you can try reading one character at a time.