android, php and returning file
<p>I write some application which cooperate with WWW server. In brief my application need to work some like that: application send some data to WWW server. Server get this data and then he return XML file, or URL to this XML file, then application download this XML file and close connection. </p>

<p>Here is my code with send file to server:</p>

<pre><code>public void sendToServer(String fileName) {
    ExtendedLog.i(TAG, "sendToServer");
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = unsendDirectory + File.separator + fileName;
    String pathToCopyFile = sendDirectory + File.separator + fileName;

    File file = new File(pathToOurFile);
    File copyFile = new File(pathToCopyFile);

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

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

    try
    {
        FileInputStream fileInputStream = new FileInputStream(file);

        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=\"uploadedfile\";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)
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();

        Log.i("ODPOWIEDZ", serverResponseMessage);

        if(sendFileProblem) {
            sendFileProblem = false;
        }

        InputStream in = new FileInputStream(file);

        //For Append the file.
        //  OutputStream out = new FileOutputStream(f2,true);

        //For Overwrite the file.
        OutputStream out = new FileOutputStream(copyFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) &gt; 0){
            out.write(buf, 0, len);
        }
        in.close();
        out.close();

        file.delete();
    }
    catch (Exception ex) {
        Log.i("WYJATEK", ex.getMessage());

        if(sendFileProblem == false) {
            healthCareApplication.showNotification(R.drawable.internet, "Send file problem", "Problem with sending file occurred",null,2,null);
            sendFileProblem = true;
        }
    }
}
</code></pre>

<p>and here is my php server: </p>

<pre><code>&lt;?php
    $target_path  = "./";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

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

<p>My problem is that I really dont know how to return some file from php script and how to download this file from java. Can anyone give me some tip how I can do it? Thanks for any help.</p>
