uploading file in android
<p>I am trying to upload a file from android emulator to remote machine. I am using this code to upload file      </p>

<pre><code>public int uploadPdfFile(String file) {
     int response = 0;
    try{

    File textFile = new File(file);
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

    HttpURLConnection connection = (HttpURLConnection) new URL("http://192.1.1.1:8080/FileUploadingProj/ReverseServlet").openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    OutputStream output = connection.getOutputStream();

    PrintWriter writer = new PrintWriter(output, true);
    // Send text file.
    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"file1\"; filename=\"" + textFile.getName() + "\"");
    writer.println("Content-Type: application/octet-stream");
    FileInputStream fin = new FileInputStream(textFile);
    writer.println();
    IOUtils.copy(fin, output);
    writer.println();
    // End of multipart/form-data.
    writer.println("--" + boundary + "--");
    output.flush();
//        closeQuietly(fin, writer, output);
    // Above request will never be sent if .getInputStream() or .getResponseCode() or .getResponseMessage() does not get called.
    response = connection.getResponseCode();
    System.out.println("==res==="+connection.getResponseCode());
    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return response;
}
</code></pre>

<p>with this code the file not getting uploaded and am getting the following error at server side
      org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly</p>

<p>I am calling the above method from my activity...but getting bad response code..</p>

<p>The same snippet of code is working fine with my java standalone  program...</p>

<p>here is my java standalone program...</p>

<pre><code>import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.io.IOUtils;

public class URLUploader {

  public static void main(String[] args) throws IOException {



    File textFile = new File("D:\\Books\\PostgreSQL-9-Admin-Cookbook-eBook11112010_78706.pdf");
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

    HttpURLConnection connection = (HttpURLConnection) new URL("http://192.1.1.1:8080/FileUploadingProj/ReverseServlet").openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    OutputStream output = connection.getOutputStream();

    PrintWriter writer = new PrintWriter(output, true);
    // Send text file.
    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"file1\"; filename=\"" + textFile.getName() + "\"");
    writer.println("Content-Type: application/octet-stream");
    FileInputStream fin = new FileInputStream(textFile);
    writer.println();
    IOUtils.copy(fin, output);
    writer.println();
    // End of multipart/form-data.
    writer.println("--" + boundary + "--");
    output.flush();
 //        closeQuietly(fin, writer, output);
    // Above request will never be sent if .getInputStream() or .getResponseCode() or .getResponseMessage() does not get called.
    System.out.println("==res==="+connection.getResponseCode());
}
</code></pre>

<p>}</p>

<p>Please help me out whats wrong with my android code...Thanks</p>
