Android Application to Send a File to Remote Server
Jumat, 30 Maret 2012
0
komentar
This simple Android application sends a file to a remote server. In this example code server runs on local host. When the Send button is clicked the file is sent to the server.
Android Client Application
SimpleClientActivity.java
main.xml
AndroidManifest.xml
Server Application
main.java
Android Client Application
SimpleClientActivity.java
/*
* This is a simple Android mobile client
* This application send any file to a remort server when the
* send button is pressed
* Author by Lak J Comspace
*/
package lakj.comspace.simpleclient;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SimpleClientActivity extends Activity {
private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File file = new File("/mnt/sdcard/input.jpg"); //create file instance
try {
client = new Socket("10.0.2.2", 4444);
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText("File Sent");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
main.xml
AndroidManifest.xml
Server Application
main.java
/*
* This is a simple server application
* This server receive a file from a Android client and save it in a given place.
* Author by Lak J Comspace
*/
package simpleserver;
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(4444); //Server socket
System.out.println("Server started. Listening to the port 4444");
clientSocket = serverSocket.accept();
byte[] mybytearray = new byte[filesize]; //create byte array to buffer the file
inputStream = clientSocket.getInputStream();
fileOutputStream = new FileOutputStream("D:\\output.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("Sever recieved the file");
}
}
Baca Selengkapnya ....