VentanaPrincipal.java Source code

Java tutorial

Introduction

Here is the source code for VentanaPrincipal.java

Source

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Valen
 */
public class VentanaPrincipal extends javax.swing.JFrame {
    FTPClient cliente;
    String servFTP;
    String usuario;
    String clave;

    /**
     * Creates new form VentanaPrincipal
     */
    public VentanaPrincipal() {
        initComponents();
        cliente = new FTPClient();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogCrearCarpeta = new javax.swing.JDialog();
        jLabel2 = new javax.swing.JLabel();
        tfNombre = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btnSubir = new javax.swing.JButton();
        btnDescargar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCrear = new javax.swing.JButton();
        btnEliminarCar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        lblMens = new javax.swing.JLabel();
        listFicheros = new java.awt.List();
        jLabel1 = new javax.swing.JLabel();
        lblRuta = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tfServidor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfUsuario = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfContrasena = new javax.swing.JTextField();
        btnConect = new javax.swing.JButton();

        dialogCrearCarpeta.setTitle("Crear carpeta");
        dialogCrearCarpeta.setModal(true);
        dialogCrearCarpeta.setSize(new java.awt.Dimension(270, 150));

        jLabel2.setText("Nombre de la carpeta: ");

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout dialogCrearCarpetaLayout = new javax.swing.GroupLayout(
                dialogCrearCarpeta.getContentPane());
        dialogCrearCarpeta.getContentPane().setLayout(dialogCrearCarpetaLayout);
        dialogCrearCarpetaLayout.setHorizontalGroup(dialogCrearCarpetaLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(dialogCrearCarpetaLayout.createSequentialGroup().addGroup(dialogCrearCarpetaLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(dialogCrearCarpetaLayout.createSequentialGroup().addContainerGap()
                                .addComponent(jLabel2))
                        .addGroup(dialogCrearCarpetaLayout.createSequentialGroup().addGap(31, 31, 31)
                                .addGroup(dialogCrearCarpetaLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(dialogCrearCarpetaLayout.createSequentialGroup()
                                                .addComponent(jButton1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButton2))
                                        .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 182,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(57, Short.MAX_VALUE)));
        dialogCrearCarpetaLayout.setVerticalGroup(dialogCrearCarpetaLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(dialogCrearCarpetaLayout.createSequentialGroup().addContainerGap().addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(dialogCrearCarpetaLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton2).addComponent(jButton1))
                        .addContainerGap(47, Short.MAX_VALUE)));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CLIENTE FTP");

        btnSubir.setText("Subir fichero");
        btnSubir.setEnabled(false);
        btnSubir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubirActionPerformed(evt);
            }
        });

        btnDescargar.setText("Descargar fichero");
        btnDescargar.setEnabled(false);
        btnDescargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDescargarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar fichero");
        btnEliminar.setEnabled(false);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnCrear.setText("Crear carpeta");
        btnCrear.setEnabled(false);
        btnCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCrearActionPerformed(evt);
            }
        });

        btnEliminarCar.setText("Eliminar carpeta");
        btnEliminarCar.setEnabled(false);
        btnEliminarCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarCarActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        lblMens.setText("jLabel1");

        listFicheros.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listFicherosMouseClicked(evt);
            }
        });

        jLabel1.setText("Ruta:");

        lblRuta.setText("jLabel2");

        jButton3.setText("");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setText("Servidor");

        tfServidor.setText("localhost");

        jLabel4.setText("Usuario");

        tfUsuario.setText("valen");

        jLabel5.setText("Contrasea");

        tfContrasena.setText("valen");

        btnConect.setText("Conectar");
        btnConect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblMens, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(jLabel1).addGap(18, 18, 18)
                                                .addComponent(lblRuta, javax.swing.GroupLayout.DEFAULT_SIZE, 257,
                                                        Short.MAX_VALUE)
                                                .addGap(18, 18, 18).addComponent(jButton3))
                                .addComponent(listFicheros, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(btnSubir, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnDescargar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        206, Short.MAX_VALUE)
                                                .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnCrear, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnEliminarCar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addContainerGap())
                                .addGroup(layout.createSequentialGroup().addGap(29, 29, 29).addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel4).addComponent(jLabel3).addComponent(jLabel5))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                                        false)
                                                .addComponent(btnConect, javax.swing.GroupLayout.DEFAULT_SIZE, 117,
                                                        Short.MAX_VALUE)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(tfServidor,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE, 117,
                                                                Short.MAX_VALUE)
                                                        .addComponent(tfUsuario).addComponent(tfContrasena)))
                                        .addContainerGap(20, Short.MAX_VALUE)))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblMens).addComponent(jLabel3).addComponent(tfServidor,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(tfUsuario, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addComponent(jLabel5).addGap(19, 19, 19)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton3).addComponent(jLabel1).addComponent(lblRuta)
                                                .addComponent(btnConect)))
                                .addComponent(tfContrasena, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(listFicheros, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup().addGap(0, 114, Short.MAX_VALUE)
                                        .addComponent(btnSubir)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnDescargar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnEliminar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnCrear)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnEliminarCar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnSalir)))
                        .addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        try {
            cliente.logout();
            cliente.disconnect();
        } catch (IOException ex) {
        }
        System.exit(0);
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnSubirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubirActionPerformed
        try {
            subirFichero();
        } catch (IOException ex) {
            lblMens.setText("Error al subir el documento");
        }
    }//GEN-LAST:event_btnSubirActionPerformed

    private void btnDescargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDescargarActionPerformed
        try {
            descargarFichero();
        } catch (IOException ex) {
            System.out.printf(ex.getMessage());
            lblMens.setText("Error al descargar el documento");
        }
    }//GEN-LAST:event_btnDescargarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        try {
            eliminarFichero();
        } catch (IOException ex) {
            Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCrearActionPerformed
        dialogCrearCarpeta.setVisible(true);
    }//GEN-LAST:event_btnCrearActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!tfNombre.getText().isEmpty()) {
            try {
                crearCarpeta();
            } catch (IOException ex) {
                lblMens.setText("Error al crear carpeta");
            }
            dialogCrearCarpeta.setVisible(false);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dialogCrearCarpeta.setVisible(false);
        tfNombre.setText("");
    }//GEN-LAST:event_jButton2ActionPerformed

    private void btnEliminarCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarCarActionPerformed
        try {
            eliminarCarpeta();
        } catch (IOException ex) {
            lblMens.setText("Error al eliminar la carpeta");
        }
    }//GEN-LAST:event_btnEliminarCarActionPerformed

    private void listFicherosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listFicherosMouseClicked
        if (evt.getClickCount() == 2) {
            String aux = listFicheros.getComponentAt(evt.getPoint()).toString();
            String nom = aux.substring(aux.lastIndexOf("=") + 1, aux.lastIndexOf("-"));

            try {
                cliente.changeWorkingDirectory(nom);
                lblRuta.setText(cliente.printWorkingDirectory());
                listFicheros.removeAll();
                FTPFile[] files = cliente.listFiles();
                String tipos[] = { "Fichero", "Directorio", "Enlace simb." };
                for (int i = 0; i < files.length; i++) {
                    listFicheros.add(files[i].getName() + "- " + tipos[files[i].getType()]);
                }
            } catch (IOException ex) {
                Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_listFicherosMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            cliente.changeToParentDirectory();
            lblRuta.setText(cliente.printWorkingDirectory());
            listFicheros.removeAll();
            FTPFile[] files = cliente.listFiles();
            String tipos[] = { "Fichero", "Directorio", "Enlace simb." };
            for (int i = 0; i < files.length; i++) {
                listFicheros.add(files[i].getName() + "- " + tipos[files[i].getType()]);
            }
        } catch (IOException ex) {
            Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btnConectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConectActionPerformed
        conectar();
    }//GEN-LAST:event_btnConectActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VentanaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentanaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentanaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentanaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConect;
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnDescargar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnEliminarCar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnSubir;
    private javax.swing.JDialog dialogCrearCarpeta;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblMens;
    private javax.swing.JLabel lblRuta;
    private java.awt.List listFicheros;
    private javax.swing.JTextField tfContrasena;
    private javax.swing.JTextField tfNombre;
    private javax.swing.JTextField tfServidor;
    private javax.swing.JTextField tfUsuario;

    // End of variables declaration//GEN-END:variables
    private void conectar() {
        if ((!tfServidor.getText().isEmpty())) {

            servFTP = tfServidor.getText();
            usuario = tfUsuario.getText();
            clave = tfContrasena.getText();
            lblMens.setText("Nos conectamos a: " + servFTP);
            try {
                cliente.connect(servFTP);
                boolean login = cliente.login(usuario, clave);
                if (login)
                    lblMens.setText("Login correcto ");
                else {
                    lblMens.setText("Login incorrecto ");
                    cliente.disconnect();
                    System.exit(0);
                }
                lblRuta.setText(cliente.printWorkingDirectory());
                FTPFile[] files = cliente.listFiles();
                String tipos[] = { "Fichero", "Directorio", "Enlace simb." };
                for (int i = 0; i < files.length; i++) {
                    listFicheros.add(files[i].getName() + "- " + tipos[files[i].getType()]);
                }
                tfServidor.setEnabled(false);
                tfUsuario.setEnabled(false);
                tfContrasena.setEnabled(false);
                btnConect.setEnabled(false);

                btnSubir.setEnabled(true);
                btnDescargar.setEnabled(true);
                btnEliminar.setEnabled(true);
                btnEliminarCar.setEnabled(true);
                btnCrear.setEnabled(true);
            } catch (IOException ex) {
                Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println(cliente.getReplyString());
            int respuesta = cliente.getReplyCode();
            if (!FTPReply.isPositiveCompletion(respuesta)) {
                try {
                    cliente.disconnect();
                } catch (IOException ex) {
                    Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("Conexin rechazada: " + respuesta);
                System.exit(0);
            }
        }
    }

    private void subirFichero() throws IOException {
        String directorio = lblRuta.getText();
        cliente.changeWorkingDirectory(directorio);
        cliente.setFileType(FTP.BINARY_FILE_TYPE);

        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            BufferedInputStream in = new BufferedInputStream(
                    new FileInputStream(chooser.getSelectedFile().getAbsolutePath()));
            cliente.storeFile(chooser.getSelectedFile().getName(), in);
            listFicheros.add(chooser.getSelectedFile().getName() + "- Fichero");
            in.close();
            lblMens.setText("Documento subido con xito");
        }
    }

    private void descargarFichero() throws IOException {
        String directorio = lblRuta.getText();
        cliente.changeWorkingDirectory(directorio);
        JFileChooser chooser = new JFileChooser();
        cliente.enterLocalPassiveMode();
        cliente.setFileType(FTP.BINARY_FILE_TYPE);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(chooser.getSelectedFile().getAbsolutePath() + "/" + listFicheros
                            .getSelectedItem().substring(0, listFicheros.getSelectedItem().lastIndexOf("-"))));
            if (cliente.retrieveFile(
                    listFicheros.getSelectedItem().substring(0, listFicheros.getSelectedItem().lastIndexOf("-")),
                    out))
                lblMens.setText("Descargado correctamente ");
            else
                lblMens.setText("No se ha podido descargar");
            out.close();
        }
    }

    private void eliminarFichero() throws IOException {
        String directorio = lblRuta.getText();
        cliente.changeWorkingDirectory(directorio);
        String direc = listFicheros.getSelectedItem();
        if (cliente.deleteFile(
                listFicheros.getSelectedItem().substring(0, listFicheros.getSelectedItem().lastIndexOf("-")))) {
            lblMens.setText("Eliminado correctamente ");
            listFicheros.remove(listFicheros.getSelectedItem());
        } else
            lblMens.setText("No se ha podido eliminar ");
    }

    private void crearCarpeta() throws IOException {
        if (cliente.makeDirectory(tfNombre.getText())) {
            lblMens.setText("Carpeta creada con xito ");
            listFicheros.add(tfNombre.getText() + "- Directorio");
        } else {
            lblMens.setText("Error al crear la carpeta ");
        }
        tfNombre.setText("");

    }

    private void eliminarCarpeta() throws IOException {
        if (cliente.removeDirectory(
                listFicheros.getSelectedItem().substring(0, listFicheros.getSelectedItem().lastIndexOf("-")))) {
            listFicheros.remove(listFicheros.getSelectedItem());
            lblMens.setText("Carpeta eliminada con xito ");
        } else {
            lblMens.setText("No se pudo eliminar carpeta compruebe que est vaca ");
        }
    }
}