我正在写一个Android应用程序,必须在wcf webservice上发送图片。 我的应用程序可以联系Web服务并为其提供图片。 但是,大小不同,我无法在网络服务上打开图片。
编辑:通过更改网络服务部分,我得到了两个完全相同的大小。但是,仍然无法打开它。
Android部分(文件大小20ko):
File img;
try {
Log.i("image", "get file");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
Log.i("call", "end build");
MultipartEntity entity = new MultipartEntity();
entity.addPart("data", new FileBody(f));
httppost.setEntity(entity);
Log.i("call", "call");
HttpResponse response = httpclient.execute(httppost);
Log.i("call", "After");
}
catch (Exception e) {
Log.i("error cal image", e.toString());
}
编辑: Webservice(文件大小20ko):
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "picture")]
public void UploadPicture(Stream image)
{
var ms = new MemoryStream();
image.CopyTo(ms);
var streamBytes = ms.ToArray();
FileStream f = new FileStream("C:\\appicture.jpg", FileMode.OpenOrCreate);
f.Write(streamBytes, 0, streamBytes.Length);
f.Close();
ms.Close();
image.Close();
}
答案 0 :(得分:1)
您以块的形式读取文件,但只写下最后一个块:
// following line is called once, should be called after each read
fileToupload.Write(bytearray, 0, bytearray.Length);
所以试试这样:
/*...*/
do
{
bytesRead = image.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
fileToupload.Write(bytearray, 0, bytesRead);
} while (bytesRead > 0);
fileToupload.Close();
fileToupload.Dispose();
/*...*/