import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


//import org.apache.log4j.PropertyConfigurator;

/*
 * Reads in files from the client and saves it onto the local disk
 */
public class DataHandler {

	private static final int file_port=8081;  
	public String getFile(DataOutputStream output, String incoming_file_string, String incoming_address_string){

		ServerSocket fileserversocket = null;
	//	PropertyConfigurator.configure("config.properties");

		try {
			output.writeUTF("Ready for file");
			fileserversocket = new ServerSocket(file_port);
			Socket file_socket = fileserversocket.accept();
			BufferedInputStream in = new BufferedInputStream( file_socket.getInputStream() );
			FileOutputStream fos = new FileOutputStream(incoming_address_string+"/"+incoming_file_string);
			BufferedOutputStream out = new BufferedOutputStream(fos);
			int i;
			while ((i = in.read()) != -1) {
				out.write(i);
			}
			out.flush();
			in.close();
			out.close();
			file_socket.close();
			fileserversocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "true";
	}
}
