流从Android文件到.NET http服务 [英] Streaming file from android to .net http service
问题描述
我试着去流从Android的文件到一个asp.net服务:
Im trying to stream 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();
}
}
和接受它像这样
String base64 = Request.Form["fileBase64"];
byte[] bytes = System.Convert.FromBase64String(base64);
我使用的HttpURLConnection,我没有得到任何异常,但接收的文件(影像)已损坏,在这个过程中。 我试着ALOT不同的流包装对,但没有运气。任何人有这样的经历? 我流等形式条目在同一个连接到这些未损坏,例如:
I use an HttpURLConnection and I dont get any exceptions but the received file(image) is corrupted in the process. I tried ALOT of different stream wrapper pairs, but no luck. Anyone have experience in this? I stream other form entries in the same connection and these arrive un-corrupted, for example
&UserID=12345
Gratefull对您有所帮助。
Gratefull for your help.
干杯!
推荐答案
解决了这个问题:
prepare文件:
Prepare the file:
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);
}
在这个网络服务器抱怨:
after this the webserver complained:
HttpException (0x80004005): Maximum request length exceeded
最大请求长度默认为4MB,所以我设置这个在web.config中:
max request-length defaults to 4mb so i set this in web.config:
<system.web>
<httpRuntime maxRequestLength="1048576"/>
</system.web
这使得文件高达1GB(!)。
Which allows files up to 1GB(!).
编辑:
忘记服务器code:
var str = Request.InputStream;
strLen = Convert.ToInt32(str.Length);
byte[] strArr = new byte[strLen];
strRead = str.Read(strArr, 0, strLen);
这篇关于流从Android文件到.NET http服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!