We are using Commons FTPClient to retrieve files from an ftp server. Our code is similar to:
FTPClient ftpClient= new FTPClient();
ftpClient.connect(server);
ftpClient.login(username, password);
FileOutputStream out = new FileOutputStream(localFile);
ftpClient.retrieveFile(remoteFile, out)
When we run this code the file is moved from the FTP Server instead of copied. Just wondering is this expected behavior?
If this is expected behavior what is the best approach to retrieve a copy of the file from the server but leave a copy of the file on the server? (We do not have access to write to the FTP Server so we cannot write the file back to the server)
Any help appreciated,
Thanks
It is very strange behavior. I have just examined the code of FTPClient and did not see something that may remove the remote file. I believe that this is a configuration of your FTP server.
To check it I'd recommend you to try other FTP client. For example unix command line utility ftp or fget or regular web browser.
I wish you good luck.
Related
I try use ftp client in my application
first one I use org.apache.commons.net.ftp.FTPClient
but when running on linux server
can login to ftp server, can't get file list
return message is 425 Failed to establish connection.
if you close the firewall can be work
well, I try to change to sun.net.ftp.FtpClient
amazing thing happened
I can got file list
both are active modes, does anyone know why there is such a difference?
FTPClient ftpClient = new FTPClient();
ftpClient.connect("host");
ftpClient.login("account", "password");
ftpClient.listFiles("path"); // can't work if you not open firewall
FtpClient ftp = FtpClient.create("host");
ftp.login("account", "password".toCharArray());
ftp.listFiles("path"); // even if you do not open the firewall can work
It seems that the relevant difference is that sun.net.ftp.FtpClient defaults to using passive (PASV) mode, but org.apache.commons.net.ftp.FTPClient defaults to active.
At least, that is my reading of the respective source codes:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/net/ftp/impl/FtpClient.java#FtpClient.0passiveMode
(line 79)
https://commons.apache.org/proper/commons-net/apidocs/src-html/org/apache/commons/net/ftp/FTPClient.html
(line 491)
You should be able to confirm this by running:
FtpClient ftp = FtpClient.create("host");
ftp.login("account", "password".toCharArray());
ftp.enablePassiveMode(false);
ftp.listFiles("path");
You should be able to use PASV mode (aka local passive mode) with the Apache FTP client; see the javadocs.
I'm using apache's commons-net 3.5 to upload files to a remote FTP server, and setting the connection encoding to UTF-8 like below, before openning the connection.
ftpClient.setAutodetectUTF8(true);
ftpClient.setControlEncoding("UTF-8");
And this is the part that sends the file
private void uploadFile(byte[] data, String path, String fileName, FTPClient ftpClient) throws IOException {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
boolean uploadSuccess = ftpClient.storeUniqueFile(fileName, new ByteArrayInputStream(data));
Here is the problem; when sending files whose name contains Turkish characters, the result file is named incorrectly. Like,
Kimlikş.pdf -> KimlikÅ.pdf
But if I use ftp4j, everything works fine. Did anyone have this before? The data ships incrorrectly from my side. I monitored the traffic with Microsoft Network Monitor. Here is the raw request:
FTP FTP:Request from Port 62642,'STOR KimlikÅ.pdf' {TCP:5879, IPv4:134}
try to use WinSCP - it was working like a charm.
https://winscp.net/eng/download.php
no FileZilla, no TotalCommander - only WinSCP.
and by copy again (for example I had about 6k files and only 50 with cyrilic) only give at question prompt "no to all" and WinSCP copies only left files. great !
5 stars to the free software !
I guess there was a bug with apache's commons-net, so I migrated to ftp4j and the problem was no more.
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>ftp4j</artifactId>
<version>1.6</version>
</dependency>
My goal is to to convert a .txt file on an FTP page to a simple String for easy manipulation.
The specific .txt file is here: ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt. It is an anonymous FTP page, so when I use my computer's browser, there's no need for a username or password.
I've tried incorporating different codes and tips from the following sources:
Reading Text File From Server on Android
http://examples.javacodegeeks.com/core-java/apache/commons/net-commons/download-file-from-ftp-server/
How to read a text file via FTP?
Reading txt file from an ftp server and returning it from AsyncTask
unable to read file from ftp in android?
Howto do a simple ftp get file on Android
http://www.javaworld.com/article/2073325/java-app-dev/java-ftp-client-libraries-reviewed.html
http://developer.android.com/reference/java/net/URLConnection.html
None of what I've tried above helped. I'm not quite sure what I'm doing wrong.
To be clear, all I want to do is to get the plain text from the posted .txt file. I have no interest in downloading said file onto my device's memory.
If you could provide me with a step-by-step explanation on how to do this, I'd be very thankful.
Ok. I've got it. For those who are in the same boat, the step-by-step answer is below:
A lot of problems other users were encountering could be solved by having permissions for internet turned on in the manifest, but mine was a little more complicated. Turns out, the main trick is to not include ftp:// in the address in Java.* Also, when you are entering an FTP site, make sure you enter via the root page, so my original page of ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt becomes: ftp.nasdaqtrader.com.
Make sure to download and include the right Apache Commons library in your project (here: http://commons.apache.org/proper/commons-net/download_net.cgi).
Connect to the root page:
FTPClient ftpClient = new FTPClient();
ftpClient.connect("ftp.nasdaqtrader.com");
Login anonymously, in this case, both username and password are "anonymous". This might be the case with many FTP pages, but I can't be sure:
ftpClient.login("anonymous", "anonymous");
Then, go to the correct directory (be careful about including/excluding the slashes):
ftpClient.changeWorkingDirectory("/SymbolDirectory");
Finally! You can now get the InputStream from the posted text file:
InputStream is = new BufferedInputStream(ftpClient.retrieveFileStream("nasdaqlisted.txt"));
Then, convert the InputStream into String and manipulate as needed. There are many ways to do this. One of which can be found here: How can I convert InputStream data to String in Android SOAP Webservices
*Source: Android FTP connection Failed
If you get "425 Unable to build data connection: Connection timed out" error, then after connecting to ftp server, I would recommend you to set the local mode to passive mode by the following statement.
ftpClient.enterLocalPassiveMode();
I want to upload file from my system to remote server as ftp.how i am to resolve this.I am using FTPClient client = new FTPClient(); and client.connect("sftp://something.com");But i am unable to connect it how i am check weather problem with my code or url.like ping command
I haven't used FTPClient (assume you mean Apache Commons FTPClient but a quick browse of apidocs shows two errors in your use:
The parameter to connect is server name, not url: client.connect("sftp://something.com") should be client.connect("something.com")
If you are using sftp you have to use FTPSClient
If you're connecting to an Sftp you might need to add the appropriate certificates (public/private keys) to the JVM where the code is running. Try your code on a regular FTP connection first, if that works, but not the sftp connections, it's almost sure is because of that.
I am trying to FTP a file on to a remote machine. Below is my code :-
FTPClient ftpClient = new FTPClient();
ftpClient.connect("home.abc.com");
ftpClient.login("remote", "guesst12");
int replyCode = ftpClient.getReplyCode();
ftpClient.changeWorkingDirectory("share"))
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream input = new FileInputStream(new File("H:/testFile.txt"));
OutputStream out = ftpClient.storeFileStream("testFile.txt");
Util.copyStream(input, out);
out.close();
input.close();
ftpClient.completePendingCommand()
ftpClient.logout();
ftpClient.disconnect();
When i execute this piece of code, the code is executed without any issues, but at the remote machine, when i check the file, the file is being created, but with no content (OKB) file. Am i missing something in code.
[Update] :
I tried with the following code for storing file :-
if(ftpClient.storeFile("testCopy.txt", input)) {
System.out.println("File Stored Successfully");
}
System.out.println(ftpClient.getReplyString());
Now the reply code i recieved is :- 451 Failure writing to local file. What does that means.
Thanks
After looking at it over and over I keep coming up with different things.
Are you sure that the InputStream is reading the file before your copying the stream? Because I'm not sure FileInputStream read's the file on initiation.
I suspect that the problem is inUtil.copyStream, which code you didn't provide. I highly recommend that you use IOutils from Apache Commons IO to copy streams.
Looking at older questions here with similar problems, it looks like you hit a bug of the Commons-NET library (of which the FTPClient is a part).
Try to install a newer version (3.0.1 or later), or an earlier version (2.2) to fix this.
One of the reasons running into FTP error 451 when trying to copy a file over FTP
especially if you see 0 sized file created on the server side,
is probably due to No Space on Disk.