By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

ftp vs sftp in JAVA

100+
P: 112
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+":"+password+"@"+host+".sbc.com:21/%2F"+remoteDirFile

Expand|Select|Wrap|Line Numbers
  1. ftpFile("upload","login","passwd","unixbox",remote,"",m_spreadName,"i");
  2.  
Expand|Select|Wrap|Line Numbers
  1. private void ftpFile(String actionType,String user,String
  2. password,String host,String remoteDirFile,String remoteFILE,String
  3. localeFile,String modeType)
  4.     {
  5.      //modeType='a' for ascii || 'i' for binary
  6.  
  7.       if(actionType.equalsIgnoreCase("download"))  ///Download a file
  8.       {
  9.        try
  10.        {
  11.           URL url = new
  12.  URL("ftp://"+user+":"+password+"@"+host+".sbc.com:21/%2F"+remoteDirFile
  13.  +";type="+modeType);
  14.  
  15.           URLConnection conn = url.openConnection();
  16.  
  17.           PrintWriter ot = new PrintWriter(new FileWriter(localeFile));
  18.           BufferedReader in = new BufferedReader(new
  19. InputStreamReader(conn.getInputStream()));
  20.           String line="";
  21.  
  22.           while((line = in.readLine()) != null)
  23.           {
  24.               ot.println(line);
  25.           }
  26.  
  27.         ot.close();
  28.         in.close();
  29.  
  30.        }
  31.        catch (MalformedURLException m)
  32.        {
  33.           System.out.println(" Error: "+m);
  34.        }
  35.        catch (IOException ioe)
  36.        {
  37.  
  38.          System.out.println(" Error: "+ioe);
  39.          String weekDay = convertToValidWeekDay(determineValidWeekDay((new GregorianCalendar().get(Calendar.DAY_OF_WEEK + day--))));
  40.  
  41.          if(day < -7)
  42.          {
  43.            System.out.println(" No valid file(s) to "+actionType);
  44.          }
  45.  
  46.          String remoteDirFile2="/production/"+weekDay+"/dir2/"+
  47.  remoteFILE;
  48.  
  49. //ftpFile(actionType,user,password,host,remoteDirFile2,remoteFILE,locale File,modeType);
  50.        }
  51.       }
  52.      else if(actionType.equalsIgnoreCase("upload"))  ///Upload a file;
  53. Only binary files
  54.       {
  55.         try
  56.         {
  57.             URL url = new
  58. URL("ftp://"+user+":"+password+"@"+host+".sbc.com:21/%2F"+remoteDirFile
  59. ); //'a' for ascii and 'i' for binary
  60.  
  61.             URLConnection conn = url.openConnection();
  62.             OutputStream os = conn.getOutputStream();
  63.             BufferedInputStream bis =  new BufferedInputStream(new
  64. FileInputStream(localeFile));
  65.             int number;
  66.  
  67.             while((number = bis.read()) != -1)
  68.             {
  69.               os.write(number);
  70.               os.flush();
  71.             }
  72.  
  73.             os.close();
  74.             bis.close();
  75.  
  76.        }
  77.        catch (MalformedURLException m)
  78.        {
  79.          System.out.println(" Error: "+m);
  80.        }
  81.        catch (IOException ioe)
  82.        {
  83.           System.out.println(" Error: "+ioe);
  84.        }
  85.       }
  86.     }
  87.  
Nov 17 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 112
I switched the line to read:


Expand|Select|Wrap|Line Numbers
  1. URL("sftp://"+user+":"+password+"@"+host+".sbc.com:21/%2F"+remoteDirFile);
  2.  
fyi..i switched the ftp:// to sftp://..

and it jumps to this exception:

Error: java.net.MalformedURLException: 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?
Nov 17 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.