FTP file content lookup

Hi Folks

My requirement is to lookup a file in an ftp folder and then read the contents of the file and extract a value based on a key.

I am passing a customer Number and the Filename to the program below to extract the value but though the FTP connection seems to be successful there is no value extracted. I am not sure if the way I am passing the fiename is correct. Please suggest. I have also attached a sample file file

for example I will be passing a customer number "123435" then I should get the value "FFFFF" as extract from the file.


public String File_value_lookup(String custName, String fileName, Container container) throws StreamTransformationException{
String custId = "";

pass 

try{
URL url = new URL("ftp://<Username>:<Password>@XXXXXXX:21//EAI_CrossAppsDev/TEST/XXXX/XXXXXXX/XXXX/"+ fileName +";type=i");

URLConnection connection = url.openConnection();
InputStream lookupStream = connection.getInputStream();

InputStreamReader reader = new InputStreamReader(lookupStream);
BufferedReader buffer = new BufferedReader(reader);


String read;

while((read=buffer.readLine())!= null){
   String temp = read.substring(0,custName.length());
     if(cusName.equals(temp))
	 custId = read.substring(custName.length()+1,read.length());
   }
  buffer.close();
}
catch(Exception e){
  return e.toString();
}  

return custId;

}

Open in new window

lookup.txt
kalyangkmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
How does your attached file work in relation to the above? (12345=1100, not FFFFF)
0
kalyangkmAuthor Commented:
Sorry, there are lot of misrepresentations. Also please find the updated code which I am trying to debug and the output should be 1100 not FFFF

package filelookup;

import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class CustomerLookup {
	 public static void main(String[] args) {
		 String custId = "";
         String custName = "12345";

		 try{
	URL url = new URL("ftp://<Username>:<Password>@XXXXXXX:21/EAI_CrossAppsDev/TEST/XXXX/XXXXXXX/XXXX/lookup.txt;type=i");

		 URLConnection connection = url.openConnection();
		 System.out.println("connection Successful:");
		 InputStream lookupStream = connection.getInputStream();
         System.out.println("Stream Read:");
		 InputStreamReader reader = new InputStreamReader(lookupStream);
		 BufferedReader buffer = new BufferedReader(reader);


		 String read;

		 while((read=buffer.readLine())!= null){
		    String temp = read.substring(0,custName.length());
		    System.out.println("custname:" +temp);
		      if(custName.equals(temp))
		 	 custId = read.substring(custName.length()+1,read.length());
		    }
		   buffer.close();
		 }
		 catch(Exception e){
		   e.toString();
		   e.printStackTrace();
		 }  

		System.out.println("custID:" +custId);
	 }
	
	

}

Open in new window


Regards
Kalyan.
0
kalyangkmAuthor Commented:
Also when I try to debug at the following output I am getting  "drwxrwxrwx" instead of a value.

System.out.println("custname:" +temp);
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

CEHJCommented:
I am getting  "drwxrwxrwx" instead of a value.
That's a permissions listing, so it suggests you're not actually giving it a proper url of a file OR you're misrepresenting the contents of the file. If the latter, could you please post a more accurate example file?
0
kalyangkmAuthor Commented:
Hi CEHJ,

Here is more precisely what I am trying right now. PFA is the sample file.

package filelookup;

import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class CustomerLookup {
	 public static void main(String[] args) {
		 String custId = "";
         String custName = "0004871464";

		 try{

URL url = new URL("ftp://<Username>:<Password>@XXXXXXX:21/EAI_CrossAppsDev/TEST/XXXX/XXXXXXX/XXXX/lookup.txt;type=i");

		 URLConnection connection = url.openConnection();
		 System.out.println("connection Successful:");
		 InputStream lookupStream = connection.getInputStream();
         System.out.println("Stream Read:");
		 InputStreamReader reader = new InputStreamReader(lookupStream);
		 BufferedReader buffer = new BufferedReader(reader);


		 String read;

		 while((read=buffer.readLine())!= null){
		    String temp = read.substring(0,custName.length());
		    System.out.println("custname:" +temp);
		      if(custName.equals(temp))
		 	 custId = read.substring(custName.length()+1,read.length());
		    }
		   buffer.close();
		 }
		 catch(Exception e){
		   e.toString();
		   e.printStackTrace();
		 }  

		System.out.println("custID:" +custId);
	 }
	
	

}

Open in new window


Regards
Kalyan.
lookup.txt
0
kalyangkmAuthor Commented:
And I am still getting the same output

custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:-rwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
custname:drwxrwxrwx
....
......

Regards
kalyan.
0
CEHJCommented:
lookup.txt is much the same as it was before
0
kalyangkmAuthor Commented:
Yes That is exactly what I am trying to use. Do you think there is something to do with the "type=i" specified in the URL?

Regards
kalyan.
0
kalyangkmAuthor Commented:
Just remove the Type category from the URL and it makes no difference. still the same output.
0
kalyangkmAuthor Commented:
I mean I removed it and it still makes no difference.
0
CEHJCommented:
Make sure the file on that server contains the content you think it does
0
kalyangkmAuthor Commented:
I see the issue, but its very strange.

I used the following code to download the lookup.txt file into another folder and to my surprise its all messed up after if got downloaded to a different folder. PFA is the screenshot of a part of the file, I cant post the other part as it has the list of other important files I cant share. But that should give you an idea of whats happening and now i see why I am getting the output I am getting.

http://stackoverflow.com/questions/14467107/java-accessing-a-file-from-an-ftp-server

Somehow it is getting all the file names and folders present in the FTP domain into the lookup.txt which is totally confusing.
screenshot.doc
0
CEHJCommented:
It might well be, but not from the code you posted, i doubt. The likelihood is that file you're trying to read is already corrupt. It's got a directory listing in it instead of the content you expect. fyi, if it really did contain name value pairs, you could read it like this
   public String fileValueLookup(String custName, String fileName,
        Container container) throws StreamTransformationException {
        String custId = "";
        InputStream in = null;

        //pass 
        try {
            URL url = new URL(
                    "ftp://<Username>:<Password>@XXXXXXX:21//EAI_CrossAppsDev/TEST/XXXX/XXXXXXX/XXXX/" +
                    fileName + ";type=i");

            Properties p = new Properties();
            in = url.openStream();
            p.load(in);

            custId = p.getProperty(custName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) { /* ignore */
            }
        }

        return custId;
    }

Open in new window

0
kalyangkmAuthor Commented:
I found the issue. There is no problem with the file I am trying to lookup as I created new file and the issue persists. The issue here is the all the details in the downloaded file screenshot that I have pasted is from the root directory of the FTP. So this is really strange as I have clearly specified the path and the second program I have used does show the path I intend to access. But somehow it keeps accessing only the root directory of the ftp host. has it anything to do with the user permissions?
0
CEHJCommented:
Maybe there's some kind of 'fallback listing' going on, but i don't see what the problem is if your user has read access
0
kalyangkmAuthor Commented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
a=1
b=2
c=3

Open in new window


fyi, i just tested the code i wrote on a remote file containing the above content and it worked perfectly
0
kalyangkmAuthor Commented:
Effective solution given by me. But there is a duplicate for this question, So you can ignore one or cancel one.

Regards
kalyan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.