Está en la página 1de 2

CLIENTE

package enviararchivo;
import java.io.*;
import java.net.*;
import javax.swing.JFileChooser;
public class EnviarArchivoCliente {
public static void main(String[] args)
{
javax.swing.JFileChooser j= new javax.swing.JFileChooser();
j.showOpenDialog(j);
int seleccion=j.showOpenDialog(j);
String path= j.getSelectedFile().getAbsolutePath();
//el entero seleccion verifica el valor de aver presionado aceptar
if (seleccion == JFileChooser.APPROVE_OPTION)
{
//seleccionamos archivo
//File fichero = j.getSelectedFile();
// y a trabajar con fichero ....
DataInputStream entrada ;
BufferedInputStream bis ;
BufferedOutputStream bos ;
int in;
byte [] byteArray;
//Ultima cadena nombre de archivo
//String NombreArchivo = "/ opt/jdk-1.6.bin" ;
String NombreArchivo = "/ opt/jdk-1.6.bin" ;
try {
//ultimo archivo n.. archivo local
File archivoLocal = new File(path);
Socket cliente = new Socket ( "localhost" , 1234 );
bis = new BufferedInputStream ( new FileInputStream ( archivoLocal ));
bos = new BufferedOutputStream ( cliente . getOutputStream ());

// Enviamos el Nombre del Archivo
DataOutputStream dos = new DataOutputStream ( cliente.getOutputStream ());
dos.writeUTF ( archivoLocal . getName ());

byteArray = new byte [8192];
while(( in = bis.read( byteArray
)) != -1 ) {
bos . write ( byteArray , 0 , in );
}

bis .close();
bos .close();

} catch ( Exception e ) {
System.err.println ( e );
}
}
}
}

SERVIDOR
package enviararchivo;
import java.io.*;
import java.net.*;
public class Servidor {
public static void main(String[] args ) throws Exception {
ServerSocket servidor ;
Socket conexion;
DataOutputStream salida ;
BufferedInputStream bis;
BufferedOutputStream bos;

byte [] receivedData ;
int in;
//Cadena de archivo ;
String archivo;
try{
servidor = new ServerSocket(1234);
while (true) {
conexion = servidor.accept();
receivedData = new byte [ 1024 ];
bis = new BufferedInputStream (conexion.getInputStream());
DataInputStream dis = new DataInputStream (conexion.getInputStream());

// Recibimos el Nombre del Fichero
archivo = dis.readUTF();
archivo = archivo.substring(archivo.indexOf ("/")+1 ,archivo.length());

bos = new BufferedOutputStream( new FileOutputStream("D:/" + archivo));
while ((in = bis.read(receivedData)) != -1) {
bos.write( receivedData , 0 , in );
}
bos.close();
dis.close();
}
} catch( Exception e ) {
System.err.println ( e );
}
}
}

También podría gustarte