FTP İşlemleri Yapan Applet

FTP protokolü ile dosya upload eden veya download eden applet uygulaması

  


import java.applet.Applet;
import java.awt.Label;
import java.net.*;
import java.io.*;

/* Baglanti
Aynı anda hem gonderme hem indirme yapmak mantıklı degil
FTP Kullanıcı adı ve sifreyi ve hostu ayarlamayı unutmayın.
class dosyası içerisinde bunları saklamak mantıklı değil.
class dosyaları geriye java dosyalarına dönüştürülebilirler.
 */

public class FTPIslemcisi extends Applet {

    Label Mesaj = new Label("Baglanti Durumu");

    public void init() {

        add(Mesaj);

        SimpleFTPClient f = new SimpleFTPClient();
        f.setHost("xyz.com");
        f.setUser("hebelehubele");
        f.setPassword("hebelehubele");
        f.setRemoteFile("public_html/linuxserver.txt");

        boolean connected = f.connect();

        // Dosya Göndermek Icin
        if (connected) {
            // Upload a file from your local drive, lets say in “c:/ftpul/u.txt"
            if (f.uploadFile("/home/ret/Desktop/linuxserver.txt")) // display the message of success if uploaded
            {
                Mesaj.setText(f.getLastSuccessMessage());
            } else {
                Mesaj.setText(f.getLastErrorMessage());
            }
        } else // Display any connection exception, if any
        {
            Mesaj.setText(f.getLastErrorMessage());
        }

        //Dosya Indirmek Icin
        if (connected) {
            // The downloaded file to be saved to the local drive
            // as mydl.txt and in the subfoler c:ftpdownloads
            if (f.downloadFile("/home/ret/Desktop/linuxserver.txt")) // display the message of success if uploaded
            {
                Mesaj.setText(f.getLastSuccessMessage());
            } else {
                Mesaj.setText(f.getLastErrorMessage());
            }
        } else // Display any connection exception, if any
        {
            Mesaj.setText(f.getLastErrorMessage());
        }
    }
}

class SimpleFTPClient {

    /** The URL connection object */
    private URLConnection m_client;
    /** The FTP host/server to be connected */
    private String host;
    /** The FTP user */
    private String user;
    /** The FTP user’s password */
    private String password;
    /** The remote file that needs to be uploaded or downloaded */
    private String remoteFile;
    /** The previous error message triggered after a method is called */
    private String erMesg;
    /** The previous success message after any method is called */
    private String succMesg;

    public SimpleFTPClient() {
    }

    /** Setter method for the FTP host/server */
    public void setHost(String host) {
        this.host = host;
    }

    /** Setter method for the FTP user */
    public void setUser(String user) {
        this.user = user;
    }

    /** Setter method for the FTP user’s password */
    public void setPassword(String p) {
        this.password = p;
    }

    /** Setter method for the remote file, this must include the sub-directory path relative
    to the user’s home directory, e.g you’e going to download a file that is within a sub directory
    called “sdir", and the file is named “d.txt", so you shall include the path as “sdir/d.txt"
     */

    public void setRemoteFile(String d) {
        this.remoteFile = d;
    }

    /** The method that connects to the remote FTP server */
    public synchronized boolean connect() {
        try {

            URL url = new URL("ftp://" + user + ":" + password + "@" + host + "/" + remoteFile + ";type=i");
            m_client = url.openConnection();
            return true;

        } catch (Exception ex) {
            StringWriter sw0 = new StringWriter();
            PrintWriter p0 = new PrintWriter(sw0, true);
            ex.printStackTrace(p0);
            erMesg = sw0.getBuffer().toString();
            return false;
        }
    }

    /** The method that returns the last message of success of any method call */
    public synchronized String getLastSuccessMessage() {
        if (succMesg == null) {
            return "";
        }
        return succMesg;
    }

    /** The method that returns the last message of error resulted from any exception of any method call */
    public synchronized String getLastErrorMessage() {
        if (erMesg == null) {
            return "";
        }
        return erMesg;
    }

    /** The method that handles file uploading, this method takes the absolute file path
    of a local file to be uploaded to the remote FTP server, and the remote file will then
    be transfered to the FTP server and saved as the relative path name specified in method setRemoteFile
    @param localfilename – the local absolute file name of the file in local hard drive that needs to
    FTP over
     */

    public synchronized boolean uploadFile(String localfilename) {
        try {

            InputStream is = new FileInputStream(localfilename);
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = m_client.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte[] buffer = new byte[1024];
            int readCount;

            while ((readCount = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, readCount);
            }
            bos.close();

            this.succMesg = "FTP transferi(Gönderme) yapıldı";

            return true;
        } catch (Exception ex) {
            StringWriter sw0 = new StringWriter();
            PrintWriter p0 = new PrintWriter(sw0, true);
            ex.printStackTrace(p0);
            erMesg = sw0.getBuffer().toString();

            return false;
        }
    }

    /** The method to download a file and save it onto the local drive of the client in the specified absolut path
    @param localfilename – the local absolute file name that the file needs to be saved as */

    public synchronized boolean downloadFile(String localfilename) {
        try {
            InputStream is = m_client.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            OutputStream os = new FileOutputStream(localfilename);
            BufferedOutputStream bos = new BufferedOutputStream(os);

            byte[] buffer = new byte[1024];
            int readCount;

            while ((readCount = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, readCount);
            }
            bos.close();
            is.close(); // close the FTP inputstream
            this.succMesg = "FTP transferi(İndirme) yapıldı.";

            return true;
        } catch (Exception ex) {
            StringWriter sw0 = new StringWriter();
            PrintWriter p0 = new PrintWriter(sw0, true);
            ex.printStackTrace(p0);
            erMesg = sw0.getBuffer().toString();

            return false;
        }
    }
}
 

FTP İşlemleri Yapan AppletFTP İşlemleri Yapan Applet

 

Başka bir örnek:

 

package org.kodejava.example.commons.net;

import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;

public class FileUploadDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {
            client.connect("ftp.domain.com");
            client.login("admin", "secret");

            //
            // Create an InputStream of the file to be uploaded
            //
            String filename = "Touch.dat";
            fis = new FileInputStream(filename);

            //
            // Store file to server
            //
            client.storeFile(filename, fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 

 

Kaynaklar

 

Yorumunuzu Ekleyin
FTP İşlemleri Yapan Applet Yorumları +1 Yorum
  • çetin
    1
    çetin
    java ya yeni başlayanlar için çok güzel örnekler elinize saglık.
    03 Ocak 2010 00:01:52, Pazar

Matris Uygulaması
Çapraz yazı
Java Applet Fontlar
Yükleniyor...