Uploading file via FTP when VPN - java

I have a straightforward program that uploads a file to a FTP server.
This works when I'm not on a VPN (Hotspot Shield).
FTPClient ftp = new FTPClient();
ftp.connect(ftpServer);
ftp.login(log,pw);
ftp.changeWorkingDirectory(dir);
File f = new File(FileName);
final InputStream is = new FileInputStream(f.getPath());
ftp.storeFile(f.getName(), is);
ftp.disconnect();
is.close();
However, when I am connected to a VPN, it runs with no errors and doesn't throw any exceptions, but the file just doesn't get uploaded.
I've tried to FTP via command window,
and when I try to mput it gives me this error:
"I won't open a connection to 'random ip address' (ony to 'another
random ip')
So now I think this is some kind of FTP restriction problem with VPN?
Anybody have any kind of experience with this?
Thanks.
EDIT:
I found a solution,
calling ftp.enterLocalPassiveMode(); before I storeFile() seems to have resolve the error.

I found a solution, calling ftp.enterLocalPassiveMode(); before I storeFile() seems to have resolve the error.

Related

Getting "Unable to determine system type - response: 500 Unknow command" when listing files on ESP8266 with Apache Commons Net

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.

ftp4j java ftpClient manage connection failures during file upload

I am trying to implement an ftpClient interface which would be able to manage network connection issues. I am using the ftp4j library for this purpose.
I can succesfully upload files BUT i have some issues when a connection failure appears during a file upload process. I would like to be able to re-upload (not append, just start again) the file just after a connection issue terminate my previous upload attempt.
The case is that if i try to re-try uploading the file some seconds or even minutes after the previous unsuccesful attemp i am getting:
it.sauronsoftware.ftp4j.FTPException [code=550, message= Access is denied. ]
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2794)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2586)
If i wait some time and then re-try, i will be able to upload the file to the FTP server.
Is it some parameter that i could set to my ftpClient:
...
FTPClient client = new FTPClient();
try {
client.setType(FTPClient.TYPE_BINARY);
client.connect(ipAddress, port);
client.login(userName, password);
System.out.println("Connected to FTP Server!");
...
}
...
if(ftpClient.isResumeSupported()) {
System.err.println("FTP Server supports resume. Trying to upload file");
ftpClient.upload(localFile, writtenBytes, new Ftp4jListener());
} else {
System.err.println("FTP Server does NOT supports resume. Trying to upload file");
ftpClient.upload(localFile, new Ftp4jListener());
}
...
for being able to overcome this "Access is denied" problem?
Any ideas what is it causing such an exception? Maybee a server parameter?

Manual FTP with commons.net.ftp

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.

FTPClient.listFiles() returns zero records

I can connect to remote FTP server on AS400 box.
When i try to get the list of files, I get no files. The same piece of code gives me records when I connect to remote FTP server which is on windows.
I tried the following things,
FTPFile [] files = ftpClient.listFiles(); // Assuming I am already in the working directory.
OR
FTPFile [] files = ftpClient.listFiles(remoteFolder);
I have tried setting the filetype as both binary and ascii, nothing works for AS400 ftp server.
Please advice.
Thanks

Wordnet::Similarity server: how to talk to it from Java?

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.

Categories