Stream file from android to .net http service

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

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

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

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

    }

}

      

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

&UserID=12345

      

Gratefull for your help.

Hooray!

+1


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);

        httppost.setEntity(aEntity);

        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:

<system.web>
    <httpRuntime maxRequestLength="1048576"/>
</system.web

      

That allows files up to 1 GB (!).

change:

Forgot server code:

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

      

+3


source







All Articles