Transmision de Archivos con Sockets en Java
programacion transferencia sockets javaAlgunos siempre tuvimos problemas en la transferencia de archivos, especialmente en Java, que a pesar de ser muy completo, a veces ese es su principal incoveniente (qué clase o componente usar). Aca les dejo un pequeño ejemplo que realmente funciona, con archivos de gran tamaño y usando Socket's en Java. El cliente envia el archivo al servidor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @author Miguel Peinado | |
* @author Timoteo Ponce | |
*/ | |
import java.net.*; | |
import java.io.*; | |
class Cliente{ | |
public static void main (String[] args){ | |
DataInputStream input; | |
BufferedInputStream bis; | |
BufferedOutputStream bos; | |
int in; | |
byte[] byteArray; | |
final String filename = "/opt/jdk-1.6.bin"; | |
try{ | |
final File localFile = new File( filename ); | |
Socket client = new Socket("10.23.3.20", 1234); | |
bis = new BufferedInputStream(new FileInputStream(localFile)); | |
bos = new BufferedOutputStream(client.getOutputStream()); | |
//enviamos el nombre del archivo | |
DataOutputStream dos=new DataOutputStream(client.getOutputStream()); | |
dos.writeUTF(localFile.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); | |
} | |
} | |
} | |
//y en la parte del Servidor: | |
/** | |
* | |
* @author Miguel Peinado | |
*/ | |
import java.net.*; | |
import java.io.*; | |
class Servidor{ | |
public static void main (String[] args){ | |
ServerSocket server; | |
Socket connection; | |
DataOutputStream output; | |
BufferedInputStream bis; | |
BufferedOutputStream bos; | |
byte[] receivedData; | |
int in; | |
String file; | |
try{ | |
server = new ServerSocket( 1234 ); | |
while ( true ) { | |
connection = server.accept(); | |
receivedData = new byte[1024]; | |
bis = new BufferedInputStream(connection.getInputStream()); | |
DataInputStream dis=new DataInputStream(connection.getInputStream()); | |
//recibimos el nombre del fichero | |
file = dis.readUTF(); | |
file = file.substring(file.indexOf('/')+1,file.length()); | |
bos = new BufferedOutputStream(new FileOutputStream(file)); | |
while ((in = bis.read(receivedData)) != -1){ | |
bos.write(receivedData,0,in); | |
} | |
bos.close(); | |
dis.close(); | |
} | |
}catch (Exception e ) { | |
System.err.println(e); | |
} | |
} | |
} |