Error while trying to upload file from android
<p>I am trying to upload a file from the simulator to a php server. While there is nothing wrong in logcat or http error log, I don't see the file where it is supposed to be. The server responds with HTTP_OK. Here is the upload function</p>

<pre><code>public void uploadPic() {
    ThreadPolicy tp = ThreadPolicy.LAX;
    StrictMode.setThreadPolicy(tp);

    Log.e("@@@@@@@@@", "Uploadpic called");
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = "/data/icon.jpg";
    String urlServer = "http://192.168.1.9/savepic2.php";
    //String urlServer = "http://winlab.rutgers.edu/~achanda";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();
    // Allow Inputs &amp; Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );

    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead &gt; 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    //serverResponseCode = connection.getResponseCode();
    Log.e("@@@@@@@", connection.getResponseMessage());

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }

}
</code></pre>

<p>Here is the PHP code</p>

<pre><code>    &lt;?php
if ($_FILES["file"]["error"] &gt; 0)
{
        echo "Error: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
}
else
{
        echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;";
        echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;";
        echo "Stored in: " . $_FILES["file"]["tmp_name"]. "&lt;br /&gt;";

        $target_path = "uploads/";

        $target_path = $target_path . basename( $_FILES["file"]["name"]);

        if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_path)) {
                echo "The file ".  basename( $_FILES["file"]["name"]).
                        " has been uploaded";
        }
        else{
                echo "There was an error uploading the file, please try again!";
        }
}
?&gt;
</code></pre>

<p>What else can go wrong here?</p>
