Sep 1, 2014

Android Application to Send a File to Remote Server

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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * This is a simple Android <span style="font-weight: bold; height: 14px;" id="r1weigszk2_9" class="r1weigszk2">mobile client</span>
 * 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 <span style="font-weight: bold; height: 14px;" id="r1weigszk2_2" class="r1weigszk2">java</span>.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
?
1
2
3
4
5
6
7
8
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
 
    <textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textappearance="?android:attr/textAppearanceLarge">
 
    <button android:id="@+id/button1" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Send">
 
 
</button></textview></linearlayout>


AndroidManifest.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<manifest android:versioncode="1" android:versionname="1.0" package="lakj.comspace.simpleclient" xmlns:android="http://schemas.android.com/apk/res/android">
 
    <uses-sdk android:minsdkversion="10">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name=".SimpleClientActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN">
 
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </activity>
    </application>
 
</uses-sdk></manifest>


Server Application
main.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * This is a simple <span style="font-weight: bold; height: 14px;" id="r1weigszk2_7" class="r1weigszk2">server application</span>
 * This server receive a file from a Android client and <span style="font-weight: bold; height: 14px;" id="r1weigszk2_3" class="r1weigszk2">save</span> 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");
 
    }
}

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib