流文件从android到.net http服务



我正在尝试将文件从android流式传输到asp.net服务:

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

并像这样接收

String base64 = Request.Form["fileBase64"];
byte[] bytes = System.Convert.FromBase64String(base64);

我使用HttpURLConnection,我没有得到任何异常,但收到的文件(图像)在这个过程中损坏。我尝试了很多不同的流包装器对,但没有运气。有人有这方面的经验吗?我在同一连接中流式传输其他表单条目,这些条目未损坏,例如

&UserID=12345

感谢你的帮助。

干杯!

解决了:

准备文件:

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

发布请求:

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

之后,web服务器报错:

HttpException (0x80004005): Maximum request length exceeded

max request-length默认为4mb,所以我在web.config中设置:

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

允许文件最大1GB(!)。

编辑:

忘记了服务器代码:

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

相关内容

  • 没有找到相关文章

最新更新