我需要将图片从 android 发送到服务 WCF。
我的安卓功能:
public void uploadPicture() throws ClientProtocolException, IOException
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SERVICE_URL_UPLOAD_PICTURE+"image24.png");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
File myfile = new File(Environment.getExternalStorageDirectory() + "/Documentation/wallpaper-3.jpg");
try{
MultipartEntity entity = new MultipartEntity();
entity.addPart("image", new FileBody(myfile, "image/png"));
httppost.setEntity(entity);
String responseString = httpclient.execute(httppost, responseHandler);
}
catch(IOException e)
{
e.printStackTrace();
}
}
和我在服务 WCF 中的运营合同
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/UploadImage/{filename}")]
void UploadImage(string filename, System.IO.Stream image);
和我在 WCF 中的功能
public void UploadImage(string filename, System.IO.Stream image)
{
FileStream fileToUpload = new FileStream("C:\\FileUpload\\" + filename, FileMode.Create);
byte[] bytearray = new byte[1000000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = image.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToUpload.Write(bytearray, 0, bytearray.Length);
fileToUpload.Close();
fileToUpload.Dispose();
}
我认为这很容易,但是当我看到保存的图像时,Windows 说我的图像不正确。
任何想法?