Progress Barlı, FTP Dosya Upload Appleti

FTP ile dosya etmek kolay ancak bunu bir progressbar (yükleniyor barı) ile yapmak biraz karışık olabilir.

Progress Barlı, FTP Dosya Upload Appleti

Bir kaç java class yapısı kullanarak yazmak daha sonra bu klasları başka yerdede kullanabilmenize imkan verir.

Arayüzün bulunduğu class dosyası, aynı zamanda appleti çalıştıran dosyamız :

package Araclarim;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class FTPVideoUpload5 extends JApplet implements ActionListener, PropertyChangeListener {

    LayoutAraclari LayoutIslemleri = new LayoutAraclari();
    DosyaIslemleriAraclari DosyaIslemleri = new DosyaIslemleriAraclari();

    String YazilacakDosyaAdi;
    String GonderilecekDosya;

    JLabel VideoBasligi = new JLabel("Video Başlığı:");
    JTextField VideoBasligiKutusu = new JTextField(100);

    JLabel VideoAciklamasi = new JLabel("Video Açıklaması:");
    JTextArea VideoAciklamaKutusu = new JTextArea(100, 100);
    //JScrollPane Kaydirma = new JScrollPane(VideoAciklamaKutusu);
    //scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    JLabel VideoDosyasi = new JLabel("Video Dosyası:");
    JButton VideoDosyasiSec = new JButton("Seçiniz");

    public JLabel Mesaj = new JLabel("Video Yüklemek İçin Yukarıdaki Alanları Doldurunuz");

    JProgressBar Yukleniyor = new JProgressBar(0, 100);

    public void init() {

        setLayout(null);

        LayoutIslemleri.LayoutYerlesticisi(10, 10, 200, 20, VideoBasligi, this);
        LayoutIslemleri.LayoutYerlesticisi(10, 30, 500, 20, VideoBasligiKutusu, this);
        LayoutIslemleri.LayoutYerlesticisi(10, 50, 200, 20, VideoAciklamasi, this);
        LayoutIslemleri.LayoutYerlesticisi(10, 70, 500, 100, VideoAciklamaKutusu, this);
        LayoutIslemleri.LayoutYerlesticisi(10, 180, 100, 20, VideoDosyasi, this);
        LayoutIslemleri.LayoutYerlesticisi(110, 180, 110, 20, VideoDosyasiSec, this);
        LayoutIslemleri.LayoutYerlesticisi(10, 220, 500, 20, Mesaj, this);

        LayoutIslemleri.LayoutYerlesticisi(10, 250, 500, 20, Yukleniyor, this);
        Yukleniyor.setStringPainted(true);

        VideoDosyasiSec.addActionListener(this);
       
    }

    public void actionPerformed(ActionEvent ae) {

        this.Mesaj.setText("Dosya Seçiliyor");

        String IzinVerilenMimeTipleri[] = {"image/jpg", "image/jpeg", "image/png"};

        DosyaIslemleri.YuklenecekDosyayiSor(IzinVerilenMimeTipleri);
        Mesaj.setText(DosyaIslemleri.DosyaIslemleriMesaj);

        FileDialog OkunanDosya = DosyaIslemleri.DosyaPenceresi;

        try {

            this.YazilacakDosyaAdi = OkunanDosya.getFile();
            this.GonderilecekDosya = OkunanDosya.getDirectory() + OkunanDosya.getFile();
            this.Mesaj.setText("Seçilen Dosya: " + this.GonderilecekDosya);

            try {
               
                this.VideoDosyasiSec.setEnabled(false);
                this.Yukleniyor.setValue(0);
               
                FTPBaglantisi FTPBaglanti = new FTPBaglantisi();
               
                if (FTPBaglanti.SunucuyaBaglan(this.YazilacakDosyaAdi)) {

                    this.Mesaj.setText("Sunucu baglantisi yapıldı, video dosyası gönderiliyor...");
                   
                    //FTPDosyaUpload FTPDosyaGonderiliyor=new FTPDosyaUpload(this.GonderilecekDosya, FTPBaglanti.FTPBagi);
                    FTPDosyaUpload FTPDosyaGonderiliyor=new FTPDosyaUpload();
                    FTPDosyaGonderiliyor.GonderilenDosya=this.GonderilecekDosya;
                    FTPDosyaGonderiliyor.FTPBag=FTPBaglanti.FTPBagi;
                    FTPDosyaGonderiliyor.FTPMesaj=this.Mesaj;
                   
                    FTPDosyaGonderiliyor.addPropertyChangeListener(this);
                    FTPDosyaGonderiliyor.execute();
                   
                    //this.Mesaj.setText(FTPDosyaGonderiliyor.Mesaj);
                }
               
               
            } catch (Exception ex) {
                this.Mesaj.setText(ex.getMessage());
            }

        } catch (Exception ex) {
            this.Mesaj.setText("Geçersiz Dosya, lütfen yeniden seçiniz. ");
             //ex.getMessage()
        }
    }
   
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("progress")) {
            int progress = (Integer) evt.getNewValue();
            Yukleniyor.setValue(progress);
            Yukleniyor.setString(progress + "%");
        }
    }
}
 

Applet Nesnelerini Applet Üzerinde Yerleştirmemizi Sağlayan class:

 


package Araclarim;

import java.awt.Component;
import javax.swing.JApplet;

public class LayoutAraclari {
 
    public void LayoutYerlesticisi(int x, int y, int En, int Boy, Component Nesne,  JApplet EklenecekZemin) {

        Nesne.setLocation(x, y);
        Nesne.setSize(En, Boy);

        EklenecekZemin.add(Nesne);
    }
}
 

   

Dosya Penceresi ve Filitreleme İçin Kullandığım class

package Araclarim;

import java.awt.FileDialog;
import java.awt.Frame;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Arrays;

public class DosyaIslemleriAraclari {

    public FileDialog DosyaPenceresi;
    public String DosyaIslemleriMesaj;

    public void YuklenecekDosyayiSor(String IzinVerilenMimeTipleri[]) {

        try {

            DosyaPenceresi = new FileDialog(new Frame(), "Lütfen Video Dosyası Seçin", FileDialog.LOAD);
            DosyaPenceresi.setVisible(true);

            String DosyaMimeTipi = DosyaMime(this.DosyaPenceresi.getDirectory() + this.DosyaPenceresi.getFile());

            if (!Arrays.asList(IzinVerilenMimeTipleri).contains(DosyaMimeTipi)) {

                 this.DosyaPenceresi=null;
                 DosyaIslemleriMesaj= "Bu Dosya Türü desteklenmiyor, lütfen tekrar dosya seçiniz.";
            }

            /*
             if (this.DosyaPenceresi.getDirectory() == null) {
             DosyaIslemleriMesaj="Dosya Yolu okunamıyor, lütfen tekrar dosya seçiniz.";
             }*/

           
        } catch (Exception ex) {
            DosyaIslemleriMesaj = ex.getMessage();
        }
    }

    public String DosyaMime(String DosyaYolu) {
        //MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        //String mimeType = mimeTypesMap.getContentType(DosyaYolu);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(DosyaYolu);

        return mimeType;
    }

    /*
     public String DosyaTuru(String Mime) {
     String Tur = "";
     if (Mime.equals("image/png")) {
     Tur = "png";
     } else if (Mime.equals("image/gif")) {
     Tur = "gif";
     } else if (Mime.equals("image/jpg") || Mime.equals("image/jpeg")) {
     Tur = "jpg";
     }
     return Tur;
     }
     */

}
 

  ve FTP İşlemlerimizi Yapan class, bu classta SwingWorker kullandık çünkü aynı anda iki iş yapılması gerekiyordu, Dosyanın Uploadı ve Progress Barın güncellenmesi:

 

package Araclarim;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class FTPBaglantisi {

    private String Sunucu = "hebelehubele.com";
    private String KullaniciAdi = "hebele";
    private String Sifre = "hubele";
    public URLConnection FTPBagi;
    public String FTPMesaj;

    public synchronized boolean SunucuyaBaglan(String SunucuyaYazilacakDosya) {

        try {

            URL url = new URL("ftp://" + KullaniciAdi + ":" + Sifre + "@" + Sunucu + "/" + SunucuyaYazilacakDosya + ";type=i");
            FTPBagi = url.openConnection();
            return true;

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

class FTPDosyaUpload extends SwingWorker {

    JProgressBar Yukleniyor;
    public JLabel FTPMesaj;
    public String GonderilenDosya;
    public URLConnection FTPBag;
    /*
    public void FTPDosyaUpload(String GonderilecekDosya, URLConnection FTPBaglantisi, JLabel Mesajlar){
        this.GonderilenDosya=GonderilecekDosya;
        this.FTPBag=FTPBaglantisi;
        this.FTPMesaj=Mesajlar;
    }
    */

   
    @Override
    protected Void doInBackground() throws Exception {

        try {

            File GidecekDosya = new File(this.GonderilenDosya);

            //InputStream is = new FileInputStream(GonderilecekDosya);
            InputStream is = new FileInputStream(GidecekDosya);
            BufferedInputStream bis = new BufferedInputStream(is);

            OutputStream os = this.FTPBag.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);

            byte[] buffer = new byte[1024];
            int readCount;
            int Yuzde = 0;
            long ToplamOkunan = 0;
            long DosyaBoyutu = GidecekDosya.length();

            while ((readCount = bis.read(buffer)) > 0) {

                bos.write(buffer, 0, readCount);

                ToplamOkunan += readCount;
                Yuzde = (int) (ToplamOkunan * 100 / DosyaBoyutu);
                setProgress(Yuzde);
            }
            bos.close();

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

    @Override
    protected void done() {
        if (!isCancelled()) {
            this.FTPMesaj.setText("FTP transferi ile Dosya yüklendi...");
        }
    }
}

/*
    public synchronized boolean DosyaIndir(String GonderilecekDosya) {
        try {
            InputStream is = FTPBagi.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            OutputStream os = new FileOutputStream(GonderilecekDosya);
            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.FTPMesaj = "FTP transferi ile dosya İndirme işlemi yapıldı.";

            return true;

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

            return false;
        }
    }
*/

 

 

 

Yorumunuzu Ekleyin


Yükleniyor...
Yükleniyor...