Java ile Ftp protokolü üzerinden dosya aktarımı

Asagidaki ornekte ftp protokolu uzerinden bir sunucuya dosya aktarimi ornegi bulunuyor ihtiyaciniz olabilir.

    import sun.net.ftp.*;
    import java.io.*;
     
    public class FtpTest {
     public static void main(String args[]) {
       String hostname = "ftp.melihsakarya.com"; //Remote FTP server: Change this
       String username = "mmmm"; //Remote user name: Change this
       String password = "123456"; //Remote user password: Change this
       String upfile = "c:/test2.jpg"; //File to upload passed on command line
       String remdir = "/http/"; //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
          System.out.println("Dosya yuklendi");
       } catch (Exception e) {
          e.printStackTrace();
       }
     }
    }
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>