Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By Norm

Thread: Help needed in File download from password protected FTP server.

  1. #1
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Post Help needed in File download from password protected FTP server.

    I tried in two ways to download a file from a FTP server. Both did not work, please help.

    FIRST

    I tried this...

    URL url = new URL("ftp://myuser:mypassword@myhost:myport/myfilePath;type=i");

    ERROR i got was "malformed URL".
    The problem i think was in "myuser"..
    myuser="updator@myexample.in"

    Thus there are two '@' character in the URL and so it might have been the error, i figured..

    Because of this error i tried another method.


    SECOND

    This one was using authenticator. (Am not sure how to use Authenticator class, but i read some codes from some site and then coded the following code)

    ERROR it showed was invalid username and password.. (But username and password are correct, i can login to the server through browser with the same username and password)

    The following was my code using Authenticator class.


    Java Code:
    package testftp;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.Authenticator;
    import java.net.MalformedURLException;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    /**
     *
     * @author Kuttappu
     */
    
    
    public class TestFTP {
         public static void main(String args[]) throws MalformedURLException,
          IOException {
        String urlString = "ftp://myexample.in";
        String username = "updator@myexample.in";  //This full string is the username. 
        String password = "mypassword";
        Authenticator.setDefault(new MyAuthenticator(username, password));
        URL url = new URL(urlString);
        InputStream content = (InputStream) url.getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(content));
        String line;
        while ((line = in.readLine()) != null) {
          System.out.println(line);
        }
        System.out.println("Done.");
      }
    
      static class MyAuthenticator extends Authenticator {
        private String username, password;
    
        public MyAuthenticator(String user, String pass) {
          username = user;
          password = pass;
        }
    
            @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          System.out.println("Requesting Host  : " + getRequestingHost());
          System.out.println("Requesting Port  : " + getRequestingPort());
          System.out.println("Requesting Prompt : " + getRequestingPrompt());
          System.out.println("Requesting Protocol: "
              + getRequestingProtocol());
          System.out.println("Requesting Scheme : " + getRequestingScheme());
          System.out.println("Requesting Site  : " + getRequestingSite());
          return new PasswordAuthentication(username, password.toCharArray());
        }
      }
    
    
    }


    Can anyone help me? File to be downloaded is in the server myexample.in
    Last edited by Fubarable; 05-20-2012 at 04:56 PM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    It looks like you need to study how to form a valid URL. Find the doc that defines URLs and try to make a valid URL.
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help needed in File download from password protected FTP server.

    Moderator edit: color tags removed around code block and replaced with [code] [/code] tags.

  4. #4
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    @Norm
    Isn't that a valid URL?
    I found same format in a couple of sites..
    Do you have any good links about valid URLs that you can provide me with?

    @Fuberable
    Thanks, i didn't know about CODE tag.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    Isn't that a valid URL?
    If you get the error: "malformed URL" the java program does not think it is a valid URL.
    Write a small test program with a URL, execute it and post the full text of the error message and the program code.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    This is the full ERROR message

    May 21, 2012 11:55:01 AM testftpprogram.TestFTPProgram main
    SEVERE: null
    java.net.MalformedURLException: For input string: "A3Flamez@a3flamez.in"
    at java.net.URL.<init>(URL.java:617)
    at java.net.URL.<init>(URL.java:480)
    at java.net.URL.<init>(URL.java:429)
    at testftpprogram.TestFTPProgram.download(TestFTPProg ram.java:48)
    at testftpprogram.TestFTPProgram.main(TestFTPProgram. java:88)
    Caused by: java.lang.NumberFormatException: For input string: "A3Flamez@a3flamez.in"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at java.net.URLStreamHandler.parseURL(URLStreamHandle r.java:217)
    at java.net.URL.<init>(URL.java:612)
    ... 4 more

    BUILD SUCCESSFUL (total time: 5 seconds)



    This was my code.

    Java Code:
    package testftpprogram;
    
    /**
     *
     * @author Kuttappu
     */
    import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class TestFTPProgram {
    
        /**
         * @param args the command line arguments
         */
        
         public void download( String ftpServer, String user, String password,
             String fileName, File destination ) throws MalformedURLException,
             IOException
       {
             StringBuffer sb = new StringBuffer( "ftp://" );
             // check for authentication else assume its anonymous access.
             
                sb.append( user );
                sb.append( ':' );
                sb.append( password );
                sb.append( '@' );
             
             sb.append( ftpServer );
             sb.append( '/' );
             sb.append( fileName );
             /*
              * type ==&gt; a=ASCII mode, i=image (binary) mode, d= file directory
              * listing
              */
             sb.append( ";type=i" );
             BufferedInputStream bis = null;
             BufferedOutputStream bos = null;
             try
             {
                URL url = new URL( sb.toString() );
                URLConnection urlc = url.openConnection();
     
                bis = new BufferedInputStream( urlc.getInputStream() );
                bos = new BufferedOutputStream( new FileOutputStream(
                      destination.getName() ) );
     
                int i;
                while ((i = bis.read()) != -1)
                {
                   bos.write( i );
                }
             }
             finally
             {
                if (bis != null)
                   try
                   {
                      bis.close();
                   }
                   catch (IOException ioe)
                   {
                   }
                if (bos != null)
                   try
                   {
                      bos.close();
                   }
                   catch (IOException ioe)
                   {
                   }
             }
          
       }
        
        public static void main(String[] args) {
            // TODO code application logic here
            TestFTPProgram tftpp=new TestFTPProgram();
            File testF=new File("D:\\testFile.ini");
            try {
                tftpp.download("a3flamez.in", "updator@a3flamez.in", "A3Flamez", "vn.ini", testF);
            } catch (MalformedURLException ex) {
                Logger.getLogger(TestFTPProgram.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TestFTPProgram.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    
       
    }

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Help needed in File download from password protected FTP server.

    Quote Originally Posted by Kuttappu View Post
    This is the full ERROR message

    May 21, 2012 11:55:01 AM testftpprogram.TestFTPProgram main
    SEVERE: null
    java.net.MalformedURLException: For input string: "A3Flamez@a3flamez.in"
    A URL String has to start with a protocol. Yours doesn't.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    Print out the fulll URL you are using at line 44. println("sb=" + sb);
    Last edited by Norm; 05-21-2012 at 01:44 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    A URL String has to start with a protocol. Yours doesn't.

    Protocol FTP is at line 24 na? starts with ftp://
    Last edited by Kuttappu; 05-21-2012 at 04:23 PM.

  10. #10
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    Quote Originally Posted by Norm View Post
    Print out the fulll URL you are using at line 44. println("sb=" + sb);
    here is the printed string:
    sb=ftp://updator@a3flamez.in:A3Flamez@a3flamez.in/vn.ini;type=i


    (the problem is with two '@' symbol?.. It cant be avoided since username cant be changed in that site
    username:updator@a3flamez.in
    password:A3Flamez)
    Last edited by Kuttappu; 05-21-2012 at 04:27 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    Did you print out the full text of the String being passed to the URL constructor?
    Please post it so everyone can see where the problem is that the constructor is finding.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    Try using Sockets and do the connection manually.

    Have you tried encoding the username so it does not have the @
    Last edited by Norm; 05-21-2012 at 04:27 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    Quote Originally Posted by Norm View Post
    Did you print out the full text of the String being passed to the URL constructor?
    Please post it so everyone can see where the problem is that the constructor is finding.

    Yes, i printed out entire URL string just as you asked.

    sb=ftp://updator@a3flamez.in:A3Flamez@www.a3flamez.in/vn.ini;type=i

  14. #14
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    Quote Originally Posted by Norm View Post
    Try using Sockets and do the connection manually.

    Have you tried encoding the username so it does not have the @

    How to encode username in URL? Will it work in authenticating?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    Look at the URLEncoder class.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    Thanks a million Norm..


    I encoded username and passed it into my download function. I downloaded the file, yay!!! :D

    tftpp.download("www.a3flamez.in", URLEncoder.encode("updator@a3flamez.in","ISO-8859-1"), "A3Flamez", "vn.ini", testF);

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Help needed in File download from password protected FTP server.

    Glad you got it to work. Now we all know.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: Help needed in File download from password protected FTP server.

    Am also so glad that i was able to do it. This is a part of my first ever Java project. :)

    Signing off, meet you later with another good problem. :D

Similar Threads

  1. Replies: 0
    Last Post: 09-14-2011, 10:44 AM
  2. Indexing Password-protected word files?
    By pleonastic in forum Lucene
    Replies: 0
    Last Post: 02-17-2011, 05:43 PM
  3. Replies: 2
    Last Post: 03-23-2010, 05:46 AM
  4. how to download file from ftp server
    By santhosh_el in forum Networking
    Replies: 0
    Last Post: 11-19-2009, 07:01 AM

Tags for this Thread

Posting Permissions

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