import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

// le client se connecte au serveur qui lui envoi un fichier
public class raspberry_server
{

			public final static int SOCKET_PORT = 8081;  
			public final static String FILE_TO_SEND = "/home/pi/JAVA/allee.jpg";  

			public static void main (String [] args ) throws IOException 
			{
					FileInputStream fis = null;
					BufferedInputStream bis = null;
					OutputStream os = null;
					ServerSocket servsock = null;
					Socket sock = null;
					
					try {
							servsock = new ServerSocket(SOCKET_PORT);
					  
							while (true)
							{
								System.out.println("Waiting...");
								try {
								  sock = servsock.accept();
								  System.out.println("Accepted connection : " + sock);
								  // send file
								  File myFile = new File (FILE_TO_SEND);
								  byte [] mybytearray  = new byte [(int)myFile.length()];
								  fis = new FileInputStream(myFile);
								  bis = new BufferedInputStream(fis);
								  bis.read(mybytearray,0,mybytearray.length);
								  os = sock.getOutputStream();
								  System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
								  os.write(mybytearray,0,mybytearray.length);
								  os.flush();
								  System.out.println("Done.");
								}
								finally {
								  if (bis != null) bis.close();
								  if (os != null) os.close();
								  if (sock!=null) sock.close();
								}
							}
					}
					
					finally {
					  if (servsock != null) servsock.close();
					}
					
			}
}




//http://stackoverflow.com/questions/14098648/android-java-tcp-client-server-file-transfer

			FileOutputStream fos = new FileOutputStream("/mnt/sdcard/Pictures/B.jpg");
			@SuppressWarnings("resource")
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			InputStream is = clientSocket.getInputStream();

			byte[] aByte = new byte[1024];
			int bytesRead;
			while ((bytesRead = is.read(aByte)) != -1) 
			{
				bos.write(aByte, 0, bytesRead);
				//Log display few parts the file size is less than 1024. I total up, the lost size caused the file received is incomplete
				System.out.println("SO sendFile" + bytesRead);
			}
			clientSocket.close();
			
			
			
			
			
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStream inputStream;
    private static FileOutputStream fileOutputStream;
    private static BufferedOutputStream bufferedOutputStream;
    private static int filesize = 10000000; // filesize temporary hardcoded 
    private static int bytesRead;
    private static int current = 0;

    public static void main(String[] args) throws IOException {


        serverSocket = new ServerSocket(8081);  //Server socket

        System.out.println("Server started. Listening to the port 8081");


        clientSocket = serverSocket.accept();


        byte[] mybytearray = new byte[filesize];    //create byte array to buffer the file

        inputStream = clientSocket.getInputStream();
        fileOutputStream = new FileOutputStream("/home/pi/JAVA/transfert.jpg");
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        System.out.println("Receiving...");

        //following lines read the input slide file byte by byte
        bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
        current = bytesRead;

        do {
            bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
            if (bytesRead >= 0) {
                current += bytesRead;
            }
        } while (bytesRead > -1);


        bufferedOutputStream.write(mybytearray, 0, current);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        inputStream.close();
        clientSocket.close();
        serverSocket.close();

        System.out.println("Server recieved the file");

    }
}			