4

Im trying to develop a simple java code which will upload some contents from local machine to a server/another machine.I used the below code

import sun.net.ftp.*;
import java.io.*;

public class SftpUpload {
 public static void main(String args[]) {
   String hostname = "some.remote.machine"; //Remote FTP server: Change this
   String username = "user"; //Remote user name: Change this
   String password = "start123"; //Remote user password: Change this
   String upfile = args[0]; //File to upload passed on command line
   String remdir = "/home/user"; //Remote directory for file upload
   FtpClient ftp = new FtpClient();
   try {
      ftp.openServer(hostname); //Connect to FTP server
      ftp.login(username, password); //Login
      ftp.binary(); //Set to binary mode transfer
      ftp.cd(remdir); //Change to remote directory
      File file = new File(upfile);
      OutputStream out = ftp.put(file.getName()); //Start upload
      InputStream in = new FileInputStream(file);
      byte c[] = new byte[4096];
      int read = 0;
      while ((read = in.read(c)) != -1 ) {
         out.write(c, 0, read);
      } //Upload finished
      in.close();
      out.close();
      ftp.closeServer(); //Close connection
   } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
   }
 }
}

But it is showing error in Line 11 as 'Cannot instantiate the type FtpClient'. Can some one help me how to rectify it.

Satheesh
  • 646
  • 1
  • 10
  • 33
  • You can try to use apache's [FtpClient#storeFile](http://commons.apache.org/net/apidocs/org/apache/commons/net/ftp/FTPClient.html) – Arvik Jul 31 '12 at 11:38

3 Answers3

2

You cannot instantiate it because sun.net.ftp.FtpClient is abstract class.

I suggest using Apache Commons Net instead of playing with sun.x packages. FTP client example can be found from here.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • tried the link you have suggested.but getting error 'The import org.apache cannot be resolved' – Satheesh Jul 31 '12 at 12:08
  • It is not part of JDK, it is external library. Consequently you also need library itself in classpath. You can download it from here: http://commons.apache.org/net/download_net.cgi – Mikko Maunu Jul 31 '12 at 12:10
  • that really helped.. now i have done something like this FTPClient client = new FTPClient(); FileInputStream fis = null; try { client.connect("some host name"); client.login("user", "pwd"); String filename = "Touch.dat"; fis = new FileInputStream(filename); client.storeFile(filename, fis); client.logout(); } When i give the server host and its username and pwd..it throws java.net.UnknownHostException..any idea bout this? – Satheesh Jul 31 '12 at 12:52
2

If you do want to use the Sun classes, use FtpClient.create(), as per the JavaDoc for this class.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

i have resolved the exception.thats because my machine is connected in a network which doesnt allow FTP connection.when i tried it in a private dongle it worked.

Satheesh
  • 646
  • 1
  • 10
  • 33