Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package paquete;
- import java.io.File;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.apache.commons.net.ftp.FTP;
- //import org.apache.commons.net.ftp.FTPFile;
- //import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.Socket;
- import javax.swing.*;
- import org.apache.commons.net.ftp.FTPClient;
- public class ClienteFTP extends JFrame implements ActionListener
- {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- String fn,filenm;
- String fc;
- String dirn="C:\\Games\\client";
- JPanel pnl;
- JLabel lbltle,lblud,boletos;
- Font fnt;
- JTextField txtfn,nboletos;
- JButton btnu,btnd;
- Socket s;
- InputStreamReader in;
- OutputStream out;
- BufferedReader br;
- PrintWriter pw;
- private JButton boton1,boton2,boton4,boton5;
- private JLabel usuario,pass; //servidor
- private JTextField users; //servi
- private JPasswordField contra;
- private JTextArea consola;
- FTPClient ftpcliente;
- public ClienteFTP(){
- super("FTP CLIENT");
- ftpcliente = new FTPClient();
- setLayout(null);
- boton1 = new JButton("UPLOAD");
- boton1.setBounds(10,100,100,30);
- add(boton1);
- boton1.addActionListener(this);
- boton2 = new JButton("LISTAR");
- boton2.setBounds(110,100,100,30);
- add(boton2);
- boton2.addActionListener(this);
- /*boton3 = new JButton("LISTAR");
- boton3.setBounds(210,100,100,30);
- add(boton3);
- boton3.addActionListener(this);*/
- boton4 = new JButton("FINALIZAR");
- boton4.setBounds(310,100,100,30);
- add(boton4);
- boton4.addActionListener(this);
- boton5 = new JButton("CONECTAR");
- boton5.setBounds(410,100,100,30);
- add(boton5);
- boton5.addActionListener(this);
- usuario = new JLabel("USUARIO");
- usuario.setBounds(10,20,90,30);
- add(usuario);
- lblud=new JLabel("DESTINO :");
- lblud.setBounds(210,20,90,30);
- add(lblud);
- pass = new JLabel("CONTRASEÑA");
- pass.setBounds(110,20,90,30);
- add(pass);
- users = new JTextField(10);
- users.setBounds(10,50,90,30);
- add(users);
- txtfn=new JTextField();
- txtfn.setBounds(210,50,90,30);
- add(txtfn);
- contra = new JPasswordField(10);
- contra.setBounds(110,50,90,30);
- add(contra);
- boletos=new JLabel("# Boletos :");
- boletos.setBounds(310,20,90,30);
- add(boletos);
- nboletos=new JTextField();
- nboletos.setBounds(310,50,90,30);
- add(nboletos);
- consola = new JTextArea();//Área de texto
- JScrollPane scrolltxt = new JScrollPane(consola);
- setLayout(null);
- scrolltxt.setBounds(10, 150, 500,100);
- //consola.setBounds(10,150,500,100);
- add(scrolltxt);
- }
- @Override
- public void actionPerformed(ActionEvent e){
- if(e.getSource()==boton1){
- setTitle("UPLOAD");//Acción en el JFrame
- FileWriter fichero = null;
- PrintWriter pw = null;
- try
- {
- fichero = new FileWriter("C:\\Games\\cliente\\"+users.getText()+".txt");
- pw = new PrintWriter(fichero);
- pw.println(users.getText()+"\n");
- pw.println(txtfn.getText()+"\n");
- pw.println(nboletos.getText()+"\n");
- } catch (Exception e1) {
- e1.printStackTrace();
- } finally {
- try {
- if (null != fichero)
- fichero.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- int replay = ftpcliente.getReplyCode();
- if(FTPReply.isPositiveCompletion(replay)){
- FileInputStream input = null;
- try {
- File file = new File("C:\\Games\\cliente\\"+users.getText()+".txt");
- input = new FileInputStream(file);
- try {
- ftpcliente.setFileType(FTP.BINARY_FILE_TYPE);
- ftpcliente.storeFile(file.getName(),input);
- } catch (IOException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- }
- ftpcliente.enterLocalPassiveMode();
- consola.append("Subió Satisfactoriamente el archivo\n");
- System.out.println("Subió satisfactoriamente el archivo\n");
- } catch (FileNotFoundException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- try {
- input.close();
- } catch (IOException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- else{
- consola.append("No se pudo subir el archivo verifica que estás conectado\n");
- System.out.println("No se pudo subir el archivo verifica que estás conectado\n");
- }
- }
- if(e.getSource()==boton2){
- setTitle("DOWNLOAD");
- File archivo = null;
- FileReader fr = null;
- BufferedReader br = null;
- int replay = ftpcliente.getReplyCode();
- if(FTPReply.isPositiveCompletion(replay)){
- FileOutputStream input = null;
- try {
- File file = new File("C:\\Games\\cliente\\lista.txt");
- input = new FileOutputStream(file);
- try {
- ftpcliente.setFileType(FTP.BINARY_FILE_TYPE);
- ftpcliente.retrieveFile(file.getName(),input);
- } catch (IOException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- }
- ftpcliente.enterLocalPassiveMode();
- consola.append("Bajó Satisfactoriamente el archivo\n");
- System.out.println("Bajó satisfactoriamente el archivo\n");
- } catch (FileNotFoundException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- try {
- input.close();
- } catch (IOException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- else{
- consola.append("No se pudo bajar la lista verifica que estás conectado\n");
- System.out.println("No se pudo bajar la lista verifica que estás conectado\n");
- }
- try {
- // Apertura del fichero y creacion de BufferedReader para poder
- // hacer una lectura comoda (disponer del metodo readLine()).
- archivo = new File ("C:\\Games\\cliente\\lista.txt");
- fr = new FileReader (archivo);
- br = new BufferedReader(fr);
- // Lectura del fichero
- String linea;
- while((linea=br.readLine())!=null)
- {
- System.out.println(linea);
- consola.append(linea+"\n");
- }
- }
- catch(Exception e1){
- e1.printStackTrace();
- }finally{
- // En el finally cerramos el fichero, para asegurarnos
- // que se cierra tanto si todo va bien como si salta
- // una excepcion.
- try{
- if( null != fr ){
- fr.close();
- }
- }catch (Exception e2){
- e2.printStackTrace();
- }
- }
- }
- if(e.getSource()==boton4){
- try {
- ftpcliente.disconnect();
- } catch (IOException ex) {
- Logger.getLogger(ClienteFTP.class.getName()).log(Level.SEVERE, null, ex);
- }
- consola.append("Se finalizó satisfactoriamente la aplicación\n");
- consola.append("Te haz desconectado satisfactoriamente del servidor\n");
- System.out.println("Se finalizó satisfactoriamente la aplicación\n");
- System.out.println("Te haz desconectado satisfactoriamente del servidor\n");
- System.exit(0);
- }
- if(e.getSource()==boton5){
- setTitle("CONECTAR"); //Accion en el JFrame
- ftpcliente = new FTPClient();
- try {
- ftpcliente.connect("127.0.0.1");
- if(ftpcliente.login(users.getText(), new String(contra.getPassword()))){
- consola.append(users.getText() );
- consola.append("conectado con el servidor\n");
- System.out.println("Conectado con el servidor\n");
- }
- else
- {
- consola.append("No te has podido conectar, inicia sesión primero\n");
- System.out.println("No te has podido conectar, inicia sesión primero\n");
- }
- }
- catch(IOException ex)
- {
- System.err.println(ex.getMessage());
- }
- }
- }
- public static void main(String[] args) {
- ClienteFTP formulario1 = new ClienteFTP();
- formulario1.setBounds(0,0,550,300);
- formulario1.setVisible(true);//Habilitar formulario
- formulario1.setLocationRelativeTo(null);
- formulario1.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment