Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 2
Registered: ‎03-22-2011
My Device: 9800
My Carrier: Verizon
Accepted Solution

socket ftp retr

New to blackberry and java (2 weeks) and I'm having some issues with getting a file from ftp. I've searched for 3 or 4 days trying to find examples and haven't had any luck. So I'm turning a ftp upload function into a download function and it's crapping out. Any help is appreciated..

 

Go get a file:

 

	//check above to see if destination file exists and if not skip this.

	try {
        	
	    Ftp.connect("servername", 21, "user", "password", true, null, null, null);
            Ftp.cwd("/zz/ph");
            Ftp.bin();
            Ftp.download("test.txt",FILE_NAME+"test.txt");
            Ftp.disconnect();
	    } 
	catch (IOException e)
	    {
	     	e.printStackTrace();
	    }

 Download function:

 

 

public static void download(String requestedFile, String destinationLocation) throws Exception
	{
    	sendLine("PASV");
    	String response = readLine();
    	if (!response.startsWith("227 ")) {
    		//error entering passive mode
    		throw new Exception(response);
    	}
    	String ip = null;
    	int port = -1;
    	int opening = response.indexOf('(');
    	int closing = response.indexOf(')', opening + 1);
    	if (closing > 0) {
    		String dataLink = response.substring(opening + 1, closing);
    		String[] tokenizer = splitString(dataLink, ',',-1);
    		//break up the response to get the ip and port from pasv
    		ip = tokenizer[0] + "." + tokenizer[1] + "." + tokenizer[2] + "." + tokenizer[3];
    		port = Integer.parseInt(tokenizer[4]) * 256 + Integer.parseInt(tokenizer[5]);
    	}
    	String url = "socket://" + ip + ":" + port;
 		sendLine("RETR " + requestedFile);
		response = readLine();
		SocketConnection dataConnection = (SocketConnection)Connector.open(url);
		InputStreamReader dataDownload = new InputStreamReader(dataConnection.openDataInputStream()); 
		try
		{
		
		int i = 0;
		while ((i = dataDownload.read()) != -1)
		{
			System.err.println(i);
		}
		//Close the streams
		dataDownload.close();
		}
		catch(Exception e)
		{
		System.err.println(requestedFile + " cannot be found.");
		} 
	}

 Debug output:

[0.0] > CWD /zz/ph
[0.0] < 250 CWD command successful.
[0.0] > TYPE I
[0.0] < 200 Type set to I.
[0.0] > PASV
[0.0] < 227 Entering Passive Mode (xxx,xxx,xxx,xxx,xxx,xxxx).
[0.0] > RETR test.txt
[0.0] < 425 Can't open data connection.

 

Halp. What the heck am I doing wrong. Am I reading rfc wrong? am I reading SocketConnection wrong?

 

 

 

 

 

New Contributor
Posts: 2
Registered: ‎03-22-2011
My Device: 9800
My Carrier: Verizon

Re: socket ftp retr

*smacks forehead* I was tired. working snippet:

 

 

String URL = "socket://"+ip+":"+port+";deviceside=true";
    	StreamConnection conn = null;
    	conn = (StreamConnection)Connector.open(URL);
 		sendLine("RETR " + requestedFile);
		response = readLine();
		char[] input = new char[255];		
		try
		{
			InputStreamReader _in = new InputStreamReader(conn.openInputStream());

		int i = 0;
		for ( i = 0; i < 255; ++i ) {
		input[i] = (char)_in.read();
		System.err.println(input[i]);
		};

		//Close the streams
		_in.close();
		conn.close();
		}
		catch(Exception e)
		{
		System.err.println(requestedFile + " cannot be found.");
		}
		return input;

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: socket ftp retr

Welcome to the forums.  Glad we could sort you out so easily.   Smiley Very Happy

 

Don't beat yourself up too much!  Smiley Sad

Trusted Contributor
Posts: 151
Registered: ‎07-20-2010
My Device: Not Specified

Re: socket ftp retr

did you use a perticular FTP library?