ftp vs sftp in JAVA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndedhia1
    New Member
    • Jan 2009
    • 112

    ftp vs sftp in JAVA

    I was hoping you could help me out with ftp vs sftp.
    Below is a method that I have that I call to ftp files from one unix box
    to another in house, but soon, we will have to ftp from here to NY so we
    have to start using sftp. I know that we have open ssh on our unix
    boxes but was wondering how different the syntax would be, going from
    ftp to sftp.


    Here is a method that I have written in my java code for ftping a file
    to a specific location.
    How can I change this so that it will sftp instead of ftp?
    Do I need to change this line frome ftp to sftp and thats it:
    URL("ftp://"+user+":"+pass word+"@"+host+" .sbc.com:21/%2F"+remoteDirF ile

    [code=java]
    ftpFile("upload ","login","pass wd","unixbox",r emote,"",m_spre adName,"i");
    [/code]

    [code=java]
    private void ftpFile(String actionType,Stri ng user,String
    password,String host,String remoteDirFile,S tring remoteFILE,Stri ng
    localeFile,Stri ng modeType)
    {
    //modeType='a' for ascii || 'i' for binary

    if(actionType.e qualsIgnoreCase ("download") ) ///Download a file
    {
    try
    {
    URL url = new
    URL("ftp://"+user+":"+pass word+"@"+host+" .sbc.com:21/%2F"+remoteDirF ile
    +";type="+modeT ype);

    URLConnection conn = url.openConnect ion();

    PrintWriter ot = new PrintWriter(new FileWriter(loca leFile));
    BufferedReader in = new BufferedReader( new
    InputStreamRead er(conn.getInpu tStream()));
    String line="";

    while((line = in.readLine()) != null)
    {
    ot.println(line );
    }

    ot.close();
    in.close();

    }
    catch (MalformedURLEx ception m)
    {
    System.out.prin tln(" Error: "+m);
    }
    catch (IOException ioe)
    {

    System.out.prin tln(" Error: "+ioe);
    String weekDay = convertToValidW eekDay(determin eValidWeekDay(( new GregorianCalend ar().get(Calend ar.DAY_OF_WEEK + day--))));

    if(day < -7)
    {
    System.out.prin tln(" No valid file(s) to "+actionTyp e);
    }

    String remoteDirFile2= "/production/"+weekDay+"/dir2/"+
    remoteFILE;

    //ftpFile(actionT ype,user,passwo rd,host,remoteD irFile2,remoteF ILE,locale File,modeType);
    }
    }
    else if(actionType.e qualsIgnoreCase ("upload")) ///Upload a file;
    Only binary files
    {
    try
    {
    URL url = new
    URL("ftp://"+user+":"+pass word+"@"+host+" .sbc.com:21/%2F"+remoteDirF ile
    ); //'a' for ascii and 'i' for binary

    URLConnection conn = url.openConnect ion();
    OutputStream os = conn.getOutputS tream();
    BufferedInputSt ream bis = new BufferedInputSt ream(new
    FileInputStream (localeFile));
    int number;

    while((number = bis.read()) != -1)
    {
    os.write(number );
    os.flush();
    }

    os.close();
    bis.close();

    }
    catch (MalformedURLEx ception m)
    {
    System.out.prin tln(" Error: "+m);
    }
    catch (IOException ioe)
    {
    System.out.prin tln(" Error: "+ioe);
    }
    }
    }
    [/code]
  • ndedhia1
    New Member
    • Jan 2009
    • 112

    #2
    I switched the line to read:


    [code=java]
    URL("sftp://"+user+":"+pass word+"@"+host+" .sbc.com:21/%2F"+remoteDirF ile);
    [/code]

    fyi..i switched the ftp:// to sftp://..

    and it jumps to this exception:

    Error: java.net.Malfor medURLException : unknown protocol: sftp



    Am I missing some import statements at the top or something?



    This is in the JavaDocs:

    Protocol handlers for the following protocols are guaranteed to exist on the search path :-

    http, https, ftp, file, and jar


    How Do I get sftp to be known as a known protocol?

    Comment

    Working...