Unable to write jpeg file on local drive recieved through stream over socket
<pre><code> client code is below

  RRAWsecurity.dataOutputStream = new                          
 DataOutputStream(RRAWsecurity.socket.getOutputStream());

 int k=0;

RRAWsecurity.dataOutputStream.writeUTF(Integer.toString(data.length));
RRAWsecurity.dataOutputStream.flush();

int len = 400;

 byte[] temp = new byte[len];

Toast.makeText(getApplicationContext(), Integer.toString(data.length),    
Toast.LENGTH_LONG).show();

for(int i=0; i&lt;=data.length/len; i++)
{
for(int j=0; j&lt;len; j++)
{
if(i==data.length/len)
{
if(j == data.length%len)
break;
}

temp[j] = data[k++];
}
//Toast.makeText(getApplicationContext(), Integer.toString(temp.length),    
Toast.LENGTH_LONG).show();
 RRAWsecurity.dataOutputStream.write((temp));
 }
 RRAWsecurity.dataOutputStream.flush();

The Server code is here

       private static DataInputStream dataInputStream;

public static void main(String[] args) throws IOException {
    // create socket
    int length_img = 0;
    byte[] temp = new byte[2];

    try {
        ServerSocket servsock = new ServerSocket(27508);
        Socket sock = servsock.accept();
        dataInputStream = new DataInputStream(sock.getInputStream());

        System.out.println("Accepted connection : " + sock);

        int imgNum = 0;

        // while (true) {
        String lengtha = dataInputStream.readUTF();
        length_img = Integer.parseInt(lengtha);
        // byte[] arr = Base64.decodeBase64(base64);
        byte[] data = new byte[length_img];
        int data_index = 0;

        int size = 0;
        System.out.print("length_img : " + length_img + "\n");
        /*
         * while (size &lt; (length_img + 1) &amp;&amp; length_img != 0) {
         * System.out.print("Here \n"); temp =
         * dataInputStream.readUTF().getBytes(); // temp =
         * Base64.decodeBase64(arr); // .getBytes();
         * 
         * for (int z = 0; z &lt; temp.length; z++) { data[data_index++] =
         * temp[z]; size++; System.out.print("size = " + size);
         * System.out.print(" data_index= " + data_index + "\n");
         * 
         * } temp = null; } System.out.print(data + "\n");
         */
        dataInputStream.read(data, 0, length_img);
        System.out.print(data + "\n");
        FileOutputStream imageOutFile = new FileOutputStream("E:\\image"
                + imgNum + ".jpeg");
        imgNum++;
        imageOutFile.write(data);
        imageOutFile.close();
        System.out.print("bublo");
        // }

    } catch (Exception e) {
        System.out.print(e);
    }
}
</code></pre>

<p>The problem is that i can receive the image on server side but the image is corrupt...when i write this data as a text file i see abnormal characters(ofcourse that would be there) but image is not seen when i store it as jpeg some corruption or problem.</p>
