Java sockets - file transfers

Created 14 December 2003 00:00

Assuming you have an open socket for doing the transfer (and without the correct try..catch error handling you require):

Server

  DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
  File f = new File("C:\temp\myimage.jpg");
  FileInputStream fis = new FileInputStream(f);
  byte[] buffer = new byte[4096];
  int len;
  while ((len = fis.read(buffer)) > 0) {
    dos.write(buffer, 0, len);  
  }  
  fis.close();
  dos.close();

Client

  DataInputStream dis = new DataInputStream(socket.getInputStream());
  File fout = new File("c:\temp\newimage.jpg");
  FileOutputStream fos = new FileOutputStream(fout);
  byte[] buffer = new byte[4096];
  int len;
  while ((len = dis.read(buffer)) > 0) {
    fos.write(buffer, 0, len);
  }
  fos.close();
  dis.close();