Stream file from android to .net http service

I am trying to transfer a file from android to an service:

private static void writeFile(File file, DataOutputStream out)
        throws IOException {

    BufferedInputStream bufferedFileIs = null;
    Base64OutputStream base64out = null;
    try {
        base64out = new Base64OutputStream(out, Base64.DEFAULT);

        FileInputStream fileIs = new FileInputStream(file);
        bufferedFileIs = new BufferedInputStream(fileIs);
        int nextByte;
        while ((nextByte = != -1) {
    } finally {
        if (bufferedFileIs != null) {   
        if(base64out != null)




and get it like this

String base64 = Request.Form["fileBase64"];

byte[] bytes = System.Convert.FromBase64String(base64);


I am using HttpURLConnection and I am not getting any exceptions, but the resulting file (image) is corrupted in the process. I've tried ALOT various pairs of thread fairings with no luck. Anyone have any experience with this? I am passing in records of a different form on the same connection and they arrive intact, like



Gratefull for your help.



source to share

1 answer

I decided:

Prepare the file:

File file = new File(LocalFilePath);

FileEntity fileentity = new FileEntity(file, "UTF-8");
HttpUtilities.postRequest(WebServiceURL, fileentity);


send request:

public static String postRequest(String url, HttpEntity aEntity)
        throws IOException {

    InputStream is = null;
    try {

        HttpClient client = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(url);


        HttpResponse response = client.execute(httppost);
        HttpEntity responseEntity = response.getEntity();

        is = responseEntity.getContent();

    } catch (Exception e) {

    return getResponse(is);


after that the webserver complained:

HttpException (0x80004005): Maximum request length exceeded


the default max request length is 4MB, so I set this in my web.config file:

    <httpRuntime maxRequestLength="1048576"/>


That allows files up to 1 GB (!).


Forgot server code:

var str = Request.InputStream;
strLen = Convert.ToInt32(str.Length);
byte[] strArr = new byte[strLen];
strRead = str.Read(strArr, 0, strLen);




All Articles