Glaas2

proyecto_redes

May 28th, 2013
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. package paquete;
  2. import java.io.File;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. import org.apache.commons.net.ftp.FTP;
  6. //import org.apache.commons.net.ftp.FTPFile;
  7. //import org.apache.commons.net.ftp.FTPFile;
  8. import org.apache.commons.net.ftp.FTPReply;
  9.  
  10. import java.awt.Font;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.io.BufferedReader;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.FileReader;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.io.OutputStream;
  22. import java.io.PrintWriter;
  23. import java.net.Socket;
  24.  
  25. import javax.swing.*;
  26.  
  27. import org.apache.commons.net.ftp.FTPClient;
  28.  
  29. public class ClienteFTP extends JFrame implements ActionListener
  30. {
  31.    /**
  32.      *
  33.      */
  34.     private static final long serialVersionUID = 1L;
  35.     String fn,filenm;
  36.     String fc;
  37.     String dirn="C:\\Games\\client";
  38.     JPanel pnl;
  39.     JLabel lbltle,lblud,boletos;
  40.     Font fnt;
  41.     JTextField txtfn,nboletos;
  42.     JButton btnu,btnd;
  43.     Socket s;
  44.     InputStreamReader in;
  45.     OutputStream out;
  46.     BufferedReader br;
  47.     PrintWriter pw;
  48. private JButton boton1,boton2,boton4,boton5;  
  49.    private JLabel usuario,pass; //servidor  
  50.    private JTextField users; //servi  
  51.    private JPasswordField contra;  
  52.    private JTextArea consola;
  53.    
  54.    FTPClient ftpcliente;
  55.  
  56.    public ClienteFTP(){
  57.        super("FTP CLIENT");
  58.        ftpcliente = new FTPClient();
  59.        
  60.        setLayout(null);
  61.        boton1 = new JButton("UPLOAD");
  62.        boton1.setBounds(10,100,100,30);
  63.        add(boton1);
  64.        boton1.addActionListener(this);
  65.        boton2 = new JButton("LISTAR");
  66.        boton2.setBounds(110,100,100,30);
  67.        add(boton2);
  68.        boton2.addActionListener(this);
  69.        /*boton3 = new JButton("LISTAR");
  70.        boton3.setBounds(210,100,100,30);
  71.        add(boton3);
  72.        boton3.addActionListener(this);*/
  73.        boton4 = new JButton("FINALIZAR");
  74.        boton4.setBounds(310,100,100,30);
  75.        add(boton4);
  76.        boton4.addActionListener(this);
  77.        boton5 = new JButton("CONECTAR");
  78.        boton5.setBounds(410,100,100,30);
  79.        add(boton5);
  80.        boton5.addActionListener(this);
  81.        usuario = new JLabel("USUARIO");
  82.        usuario.setBounds(10,20,90,30);
  83.        add(usuario);
  84.        lblud=new JLabel("DESTINO :");
  85.        lblud.setBounds(210,20,90,30);
  86.        add(lblud);
  87.        pass = new JLabel("CONTRASEÑA");
  88.        pass.setBounds(110,20,90,30);
  89.        add(pass);
  90.        users = new JTextField(10);  
  91.        users.setBounds(10,50,90,30);
  92.        add(users);
  93.        txtfn=new JTextField();
  94.        txtfn.setBounds(210,50,90,30);
  95.        add(txtfn);
  96.        contra = new JPasswordField(10);
  97.        contra.setBounds(110,50,90,30);
  98.        add(contra);
  99.        boletos=new JLabel("# Boletos :");
  100.        boletos.setBounds(310,20,90,30);
  101.        add(boletos);
  102.        nboletos=new JTextField();
  103.        nboletos.setBounds(310,50,90,30);
  104.        add(nboletos);
  105.        
  106.      
  107.        consola = new JTextArea();//Área de texto
  108.        JScrollPane scrolltxt = new JScrollPane(consola);
  109.        setLayout(null);
  110.        
  111.         scrolltxt.setBounds(10, 150, 500,100); 
  112.        //consola.setBounds(10,150,500,100);      
  113.        add(scrolltxt);
  114.    }
  115.     @Override
  116.    public void actionPerformed(ActionEvent e){
  117.        
  118.         if(e.getSource()==boton1){
  119.            
  120.             setTitle("UPLOAD");//Acción en el JFrame
  121.             FileWriter fichero = null;
  122.             PrintWriter pw = null;
  123.             try
  124.             {
  125.                 fichero = new FileWriter("C:\\Games\\cliente\\"+users.getText()+".txt");
  126.                 pw = new PrintWriter(fichero);
  127.      
  128.                     pw.println(users.getText()+"\n");
  129.                     pw.println(txtfn.getText()+"\n");
  130.                     pw.println(nboletos.getText()+"\n");
  131.      
  132.             } catch (Exception e1) {
  133.                 e1.printStackTrace();
  134.             } finally {
  135.                try {
  136.                if (null != fichero)
  137.                   fichero.close();
  138.                } catch (Exception e2) {
  139.                   e2.printStackTrace();
  140.                }
  141.             }
  142.             int replay = ftpcliente.getReplyCode();
  143.             if(FTPReply.isPositiveCompletion(replay)){
  144.                 FileInputStream input = null;
  145.                 try {
  146.                     File file = new File("C:\\Games\\cliente\\"+users.getText()+".txt");
  147.                     input = new FileInputStream(file);
  148.                     try {
  149.                         ftpcliente.setFileType(FTP.BINARY_FILE_TYPE);
  150.                         ftpcliente.storeFile(file.getName(),input);
  151.                     } catch (IOException ex) {
  152.                         Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  153.                     }
  154.          
  155.                      ftpcliente.enterLocalPassiveMode();
  156.                      consola.append("Subió Satisfactoriamente el archivo\n");
  157.                      System.out.println("Subió satisfactoriamente el archivo\n");
  158.                      
  159.                 } catch (FileNotFoundException ex) {
  160.                     Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  161.                 } finally {
  162.                     try {
  163.                         input.close();
  164.                     } catch (IOException ex) {
  165.                         Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  166.                     }
  167.                 }
  168.             }
  169.             else{
  170.                 consola.append("No se pudo subir el archivo verifica que estás conectado\n");
  171.                 System.out.println("No se pudo subir el archivo verifica que estás conectado\n");
  172.             }
  173.         }
  174.        if(e.getSource()==boton2){
  175.            
  176.            setTitle("DOWNLOAD");
  177.            File archivo = null;
  178.            FileReader fr = null;
  179.            BufferedReader br = null;
  180.            int replay = ftpcliente.getReplyCode();
  181.            if(FTPReply.isPositiveCompletion(replay)){
  182.                FileOutputStream input = null;
  183.                try {
  184.                    File file = new File("C:\\Games\\cliente\\lista.txt");
  185.                    input = new FileOutputStream(file);
  186.                    try {
  187.                        ftpcliente.setFileType(FTP.BINARY_FILE_TYPE);
  188.                        ftpcliente.retrieveFile(file.getName(),input);
  189.                    } catch (IOException ex) {
  190.                        Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  191.                    }
  192.          
  193.                     ftpcliente.enterLocalPassiveMode();
  194.                     consola.append("Bajó Satisfactoriamente el archivo\n");
  195.                     System.out.println("Bajó satisfactoriamente el archivo\n");
  196.                    
  197.                } catch (FileNotFoundException ex) {
  198.                    Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  199.                } finally {
  200.                    try {
  201.                        input.close();
  202.                    } catch (IOException ex) {
  203.                        Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  204.                    }
  205.                }
  206.            }
  207.            else{
  208.                consola.append("No se pudo bajar la lista verifica que estás conectado\n");
  209.                System.out.println("No se pudo bajar la lista verifica que estás conectado\n");
  210.            }
  211.            
  212.            try {
  213.                // Apertura del fichero y creacion de BufferedReader para poder
  214.                // hacer una lectura comoda (disponer del metodo readLine()).
  215.                archivo = new File ("C:\\Games\\cliente\\lista.txt");
  216.                fr = new FileReader (archivo);
  217.                br = new BufferedReader(fr);
  218.        
  219.                // Lectura del fichero
  220.                String linea;
  221.                while((linea=br.readLine())!=null)
  222.                {
  223.                   System.out.println(linea);
  224.                   consola.append(linea+"\n");
  225.                }
  226.             }
  227.             catch(Exception e1){
  228.                e1.printStackTrace();
  229.             }finally{
  230.                // En el finally cerramos el fichero, para asegurarnos
  231.                // que se cierra tanto si todo va bien como si salta
  232.                // una excepcion.
  233.                try{                    
  234.                   if( null != fr ){  
  235.                      fr.close();    
  236.                   }                  
  237.                }catch (Exception e2){
  238.                   e2.printStackTrace();
  239.                }
  240.             }
  241.        }
  242.        
  243.        if(e.getSource()==boton4){
  244.            try {
  245.                ftpcliente.disconnect();
  246.            } catch (IOException ex) {
  247.                Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
  248.            }
  249.            consola.append("Se finalizó satisfactoriamente la aplicación\n");
  250.            consola.append("Te haz desconectado satisfactoriamente del servidor\n");
  251.            System.out.println("Se finalizó satisfactoriamente la aplicación\n");
  252.            System.out.println("Te haz desconectado satisfactoriamente del servidor\n");
  253.            System.exit(0);
  254.        }
  255. if(e.getSource()==boton5){
  256.            
  257.            setTitle("CONECTAR"); //Accion en el JFrame
  258.            
  259.            ftpcliente = new FTPClient();
  260.            try {
  261.                ftpcliente.connect("127.0.0.1");
  262.                if(ftpcliente.login(users.getText(), new String(contra.getPassword()))){
  263.                    consola.append(users.getText() );
  264.                    consola.append("conectado con el servidor\n");
  265.                    System.out.println("Conectado con el servidor\n");
  266.                }
  267.                else
  268.                {
  269.                    consola.append("No te has podido conectar, inicia sesión primero\n");
  270.                    System.out.println("No te has podido conectar, inicia sesión primero\n");
  271.                }        
  272.            }
  273.            catch(IOException ex)
  274.            {
  275.                System.err.println(ex.getMessage());
  276.            }
  277.    }
  278.    }
  279.     public static void main(String[] args) {
  280.         ClienteFTP formulario1 = new ClienteFTP();
  281.         formulario1.setBounds(0,0,550,300);
  282.         formulario1.setVisible(true);//Habilitar formulario
  283.         formulario1.setLocationRelativeTo(null);
  284.         formulario1.setDefaultCloseOperation(EXIT_ON_CLOSE);
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment