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();
Related
I have a requirement to pull the files and push the files from FTP Server but I am getting the below error while pulling/fetching the files from/into FTP server. I am using Talend Open Source Data Integration Tool, also tried using custom java code and getting the same error. While I am able to pull and fetch the files using FileZilla FTP Client. I am getting the error when I am trying to pull and push the files.
Error Start ..
com.enterprisedt.net.ftp.ControlChannelIOException: Connection reset
at
com.enterprisedt.net.ftp.FTPControlSocket.readLine(FTPControlSocket.java:1014)
at
com.enterprisedt.net.ftp.FTPControlSocket.readReply(FTPControlSocket.java:1049)
at
com.enterprisedt.net.ftp.FTPControlSocket.sendCommand(FTPControlSocket.java:973)
at
com.enterprisedt.net.ftp.FTPControlSocket.createDataSocketPASV(FTPControlSocket.java:807)
at
com.enterprisedt.net.ftp.FTPControlSocket.createDataSocket(FTPControlSocket.java:563)
at
com.enterprisedt.net.ftp.FTPClient.setupDataSocket(FTPClient.java:2561)
at com.enterprisedt.net.ftp.FTPClient.dir(FTPClient.java:3468) at
vikas_sir.ftp_salesforce_and_vice_verasa_0_1.FTP_Salesforce_AND_Vice_Verasa.tFTPFileList_2Process(FTP_Salesforce_AND_Vice_Verasa.java:488) at
vikas_sir.ftp_salesforce_and_vice_verasa_0_1.FTP_Salesforce_AND_Vice_Verasa.tFTPConnection_1Process(FTP_Salesforce_AND_Vice_Verasa.java:396)
at
vikas_sir.ftp_salesforce_and_vice_verasa_0_1.FTP_Salesforce_AND_Vice_Verasa.runJobInTOS(FTP_Salesforce_AND_Vice_Verasa.java:1085) at
vikas_sir.ftp_salesforce_and_vice_verasa_0_1.FTP_Salesforce_AND_Vice_Verasa.main(FTP_Salesforce_AND_Vice_Verasa.java:942)
Error End ..
I have already tried the below things :
Allowed java application for my firewall e.g java.exe, javaw.exe and etc.
Also tried by disabling the firewall.
used netsh advfirewall set global StatefulFTP disable command as admin.
Disabled my antivirus.
Also added TLS1.1 and TLS 1.2 into .ini file.
Also tried in different Local Machines.
Tried by writing custom java code.
Below is link to the screenshot of the talend job :
Talend Job to pull the files from FTP server
Also when I am trying another FTP credentials, not getting(Can push and pull the files from/into FTP Server) any error.
Please let me know what/where I am doing wrong.
Any help will be greatly appreciated.
Thanks in Advance.
Amit
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>
I'm using apache.commons library for uploading file to a ftp server. It uploads file successful, but then I want to have a link to that file, that anyone can download it. How do I get the URL of uploaded file ?
Thanks.
One of the great features of an FTP server is the ability to use direct FTP links. With a direct link, you can use links to files and directories in emails and on your websites. Think of this: instead of sending a big attachment in an email, send a link to the file instead.
The format is really simple:
ftp:// username [colon] password [at] server.exavault.com [slash] folder-or-filename
For example: ftp://links:pass#test.exavault.com/transfer.pdf
source : http://www.exavault.com/docs/help/faq/transferring/direct-links
Folks, here is the situation:
confirmed that the xlsx file is good on the disc.
I copied the file from server and can open without problems.
using FileInputStream and BufferedInputStream to handle the client side download function.
The download function i mean user can download the file by clicking a hyperlink, and a servelet call was made to the java class which uses FileInputStream and BufferedInputStream
Mime type was set correctly as application/vnd.openxmlformats-officedocument.spreadsheetml.shee
After download the file successfully, it will give a "converted failed" error while trying to use office 2003 to open this xlsx file.
any thoughts?
Thanks!
There are bytes which didn't belong in the HTTP response body or were simply missing there.
It's impossible to point out the actual root cause based on the information given as far. You have to check if the right bytes were written from local disk file system to the HTTP response body.
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.