Results 1 to 11 of 11
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Reading content from an ftp file

    Hello. I am trying to read a content of file downloaded from ftp and return it as a String. This is what I use:
    Java Code:
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.commons.net.ftp.FTPClient;
    
    
    public class Test {
    
    
    	public static void main(String [] args) throws IOException	{
    		FTPClient ftp = new FTPClient();
    		ftp.connect("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/");
    		InputStream in = ftp.retrieveFileStream("KLAS.TXT");
    		
    		BufferedInputStream inbf = new BufferedInputStream(in);
    		
    	    int bytesRead;
    		byte[] buffer=new byte[1024]; 
    		String fileContent=null;
    		
    		while((bytesRead=inbf.read(buffer))!=-1)
    		   {
    		       fileContent=new String(buffer,0,bytesRead); 
    		   }
    		   
    		   System.out.println("File: " + fileContent);
    		}
    }
    The code does not work, although all the links are correct. Thanks a lot for your help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default Re: Reading content from an ftp file

    For one thing, you're over-writing the content of the fileContent String with each iteration of the loop. For another, if the data is truly String data, consider wrapping your InputStream in a InputStreamReader and that in a BufferedReader which should make getting text data much easier and more intuitive and less error prone.

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    Fubarable. Thank you very much! I changed the code based on your suggestion:

    Java Code:
    public static void main(String [] args) throws IOException	{
    	       FTPClient ftp = new FTPClient();
    	       InputStream in = ftp.retrieveFileStream("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT");
    	       InputStreamReader isr = new InputStreamReader(in);
    	       BufferedReader br = new BufferedReader(isr);
    	       
    	       int bytesRead = 0;        
    	       byte[] buffer=new byte[1024];         
    	       String fileContent=null;
    	       
    	       fileContent=new String(buffer,0,bytesRead);
    	       
    	       System.out.println(fileContent);
    	}
    }
    No, it is not working either:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at org.apache.commons.net.SocketClient.getRemoteAddress(SocketClient.java:651)
    	at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:732)
    	at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1853)
    	at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1844)
    	at Test.main(Test.java:16)
    When I open the file in IE, the file opens just fine, so the link is good. No idea what I am doing wrong! Thanks!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default Re: Reading content from an ftp file

    If you create a BufferedReader, consider using that object to read in your data rather than simply ignoring it. As to your NPE, what is happening on line 16 of Test.java?

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    Thanks. New version:
    Java Code:
    public static void main(String [] args) throws IOException	{
    	       FTPClient ftp = new FTPClient();
    	       InputStream in = ftp.retrieveFileStream("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT");
    	       InputStreamReader isr = new InputStreamReader(in);
    	       BufferedReader br = new BufferedReader(isr);
    	       
    	   
    	       String fileContent= br.readLine();
    	       
    	       System.out.println(fileContent);
    Line 16 is pointing to the InputStream line (with the ftp address). I wonder if I am connecting to the ftp correctly...

  6. #6
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    This worked!!!!!!!
    Java Code:
    public class Test1 {
    
    	public final static String server = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations";
    	public final static String fileName = "UAAA.TXT";
    	
    	
    	public static void main(String[] args) {
    
    		   System.out.println("Connecting to FTP server...");
    
    		    // Connection String
    		    URL url;
    	
    		    try {
    		    	url = new URL(server + "/" + fileName);
    
    		        URLConnection con = url.openConnection();
    		        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    
    		        System.out.println("Reading file start.");
    
    		        String inputLine;
    		        while ((inputLine = in.readLine()) != null)
    		            System.out.println(inputLine);
    		        in.close();
    		        }
    		        catch (FileNotFoundException e) {
    		            System.out.println("File not find on server.");
    		            System.exit(0);
    		        }catch (Exception e) {
    		            e.printStackTrace();
    		        }
    		        System.out.println("Read File Complete.");
    
    		}
    
    }
    Thanks for your support!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default Re: Reading content from an ftp file

    I would declare the BufferedReader before the try block setting it initially to null, and would close the BufferedReader in a finally block after checking first that it's not null. Otherwise it looks good.

  8. #8
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    I did it. Great. Thank you so much!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default Re: Reading content from an ftp file

    Great and you're welcome!

  10. #10
    hanuman is offline Member
    Join Date
    Feb 2015
    Posts
    1
    Rep Power
    0

    Default Re: Reading content from an ftp file

    I have the same Problem. I want to read file from FTP. I wrote the code like below. But ftpClient.retrieveFileStream(file.getName()) alwas return null.
    please give me your full code with ftp connection.
    My Code is:

    public static void main(String[] args) {
    // String source ="/files/";
    String server = "10.1.151.239";
    int port = 21;
    String user = "pavan";
    String pass = "solix123!";

    FTPClient ftpClient = new FTPClient();

    try {
    // connect and login to the server
    ftpClient.connect(server, port);
    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();


    try{

    FTPFile[] files1=ftpClient.listFiles("/TESTCASE");



    for (FTPFile file : files1) {
    String file2 = file.getName();
    System.out.println("this is file name"+ file2);

    if( file.isFile() && ftpClient.setFileType(FTP.BINARY_FILE_TYPE)){

    try {
    System.out.println("this is revrive--"+file.getName());
    ftpClient.connect(server, port);
    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();
    // System.out.println(ftpClient.getReply());
    String t = ftpClient.getReplyString();
    System.out.println(ftpClient.getReplyCode());
    System.out.println("this is repleystring--"+t);
    /*int response = ftpClient.getReply();
    if (response != FTPReply.CLOSING_DATA_CONNECTION){
    //TODO
    }*/
    InputStream stream = ftpClient.retrieveFileStream(file.getName();
    System.out.println("this is steream object ---"+stream);


    Thanks Advance

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,930
    Rep Power
    7

    Default Re: Reading content from an ftp file

    The thread is from 2013. I wouldn't hold my breath waiting for someone to meet your demands.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Reading structured content from PDF file
    By chanduk in forum Advanced Java
    Replies: 1
    Last Post: 12-09-2010, 02:03 PM
  3. how to read content of .xls file
    By kirtichopra2003 in forum Advanced Java
    Replies: 10
    Last Post: 09-11-2009, 01:03 PM
  4. Reading Body Content
    By muhfak in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 06-24-2008, 11:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •