java remote file exception

wsyy
wsyy used Ask the Experts™
on
Hi,

I am using ftp to get a remote file, and some exception occurred:

java.io.IOException: sun.net.ftp.FtpProtocolException: Welcome message: SSH-2.0-OpenSSH_4.3

My code is as follow:

public static void main(String[] args) {
            URL url;
            URLConnection connection;
            String urlString = "ftp://root:test123456@10.168.2.39:22/test.txt";
            String matchString = "name";
            String inputLine;
            BufferedReader inStream;

            try {
                  url = new URL(urlString);
            } catch (MalformedURLException e) {
                  System.err.println("Exception: " + e);
                  return;
            }
            try {
                  connection = url.openConnection();
                  inStream = new BufferedReader(new InputStreamReader(
                              connection.getInputStream()));
                  while (null != (inputLine = inStream.readLine())) {
                        if (inputLine.indexOf(matchString) >= 0) {
                              System.out.println(inputLine);
                        }
                  }
                  inStream.close();
            } catch (IOException e) {
                  System.err.println("Exception: " + e);
            }
      }

Please help.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
I would guess you should be using sftp. Ftp is deprecated and best practice is not to use it at all. The most obvious and easy thing to try is

String urlString = "sftp://root:test123456@10.168.2.39:22/test.txt";

Open in new window


but i've no idea if that's supported or will work

You could also try scp
That error is telling you that you are trying to connect to an SFTP server. SFTP and FTP are completely different protocols.

Here is an article explaining the difference:

http://www.experts-exchange.com/Software/Internet_Email/File_Sharing/FTP/A_8489-Security-of-SFTP-vs-FTPS.html

You have three options:

1. See if that server allows FTP connections (most likely on port 21)

2. Use a Java library that supports making an SFTP connection

3. Use a 3rd party solution which supports SFTP to automate the file transfer


For example, you can use Robo-FTP (http://www.robo-ftp.com) to manage making the connection and uploading / downloading files. You can call Robo-FTP from your program and have Robo-FTP pass back custom exit codes to tell your program the results of the session (success / failure).

Author

Commented:
Now, I have changed to use zehon to connect to the server. The exception now is:


Thank you for using Zehon software (www.zehon.com).  
Please go to http://www.zehon.com/feedback.php to provide us your feedback about the product.
Please go to http://www.zehon.com/feature_request.php to submit bugs or feature requests.
220 (vsFTPd 2.0.5)
AUTH TLS
234 Proceed with negotiation.
USER root
530 Permission denied.
com.zehon.exception.FileTransferException: com.zehon.exception.FileTransferException: java.lang.NullPointerException
      at com.zehon.FileTransferClient.getFile(FileTransferClient.java:407)
      at com.zehon.ftps.FTPsClient.getFile(FTPsClient.java:480)
      at com.zehon.ftps.FTPs.getFile(FTPs.java:187)
      at com.yyt.app.kb.test.ReadRemoteFile.main(ReadRemoteFile.java:46)
Caused by: com.zehon.exception.FileTransferException: java.lang.NullPointerException
      at com.zehon.ftps.FTPsClient.getFileAsStream(FTPsClient.java:427)
      at com.zehon.FileTransferClient.getFile(FileTransferClient.java:403)
      ... 3 more
Caused by: java.lang.NullPointerException
      at com.zehon.ftps.FTPsClient.getFileAsStreamHelper(FTPsClient.java:437)
      at com.zehon.ftps.FTPsClient.getFileAsStream(FTPsClient.java:425)
      ... 4 more


What does it mean? I am using root account to connect? Why does a permission error occur?
I do not think you will be able to ftp with the user 'root' . Instead use an account that has root privileges . First try to FTP with your user account to eliminate  possibility of problem from the java program itself
Top Expert 2016
Commented:
Yes, you need to check carefully that the credentials are correct.

It's a security no-no to allow root login, especially remotely, so it's unlikely, as sharonseth says, that 'root' will work.

You might also look at the API - it's not a very good sign of quality if improper authentication causes NPEs

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial