0

Android から WCF サーバーに画像を送信する作業を行っています。FileBOdy をマルチパート ボディで送信しようとしましたが、うまくいきませんでした。最後に、マルチパートボディで ByteArrayBody を送信してみました。うまくいきましたが、サーバーで破損したイメージを取得しました。私はたくさんグーグルで検索しましたが、私の問題に対する受け入れられる解決策を得ることができませんでした. Android または WCF コードの間違いを見つけられる人はいますか?

Android コード

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();

// Making HTTP request
try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();

    String URL1 = "http://rohit-pc:8078/service1.svc/UploadImage";

    HttpPost httpPost = new HttpPost(URL1);

    ContentBody bin = null;
    MultipartEntity reqEntity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");

    reqEntity.addPart("image", bab);
    reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));

    httpPost.setEntity(reqEntity);

    HttpResponse response = httpClient.execute(httpPost);
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            response.getEntity().getContent(), "UTF-8"));
    String sResponse;
     s = new StringBuilder();

    while ((sResponse = reader.readLine()) != null) {
        s = s.append(sResponse);
    }
    System.out.println("Response: " + s);
} catch (Exception e) {
    Log.e(e.getClass().getName(), e.getMessage());
}

WCF コード

public string GetStream(Stream str,string filename) {

        Guid guid = Guid.NewGuid();
        string Path = System.Web.Hosting.HostingEnvironment.MapPath("~/Images");
        FileStream file = new FileStream(Path + "/" +filename, FileMode.Create);

        byte[] bytearray = new byte[100000000];

        int bytesRead, totalBytesRead = 0;
        do {
            bytesRead = str.Read(bytearray, 0, bytearray.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

        file.Write(bytearray, 0, bytearray.Length);
        file.Close();
        file.Dispose();

       return "Success";
    }
4

2 に答える 2

0

古い質問に答えてすみません。しかし、私は問題を解決するために 5 時間以上を費やしました。そこで、私が見つけた解決策を共有したいと思います。私の問題は、サーバーに保存された画像が破損していたことです。

実際、WCF にはマルチパート フォーム データを効果的に解析する方法がありません。そのため、MS からの提案は、raw ストリームを使用して画像を wcf サービスに送信することです。

そのため、MultipartEntity (ByteArrayEntity に置き換えられた) を取り除くのに数時間苦労した後、以下のコードを使用して最終的に機能するようになりました。これが誰かを助けることを願っています。

アンドロイド

Bitmap bm = BitmapFactory.decodeFile(params[0]);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

bm.compress(CompressFormat.JPEG, 75, bos);

byte[] data = bos.toByteArray();
// the below is the important one, notice no multipart here just the raw image data 
request.setEntity(new ByteArrayEntity(data));

その後、実際の http クライアントの残りの部分が続行されます。

Wcf インターフェイス

<OperationContract()>
<WebInvoke(Method:="POST",
     ResponseFormat:=WebMessageFormat.Json,
     BodyStyle:=WebMessageBodyStyle.Bare,
     UriTemplate:="/UploadPhoto?UsedCarID={UsedCarID}&FileName={FileName}")>
Sub UploadPhoto(UsedCarID As Integer, FileName As String, FileContents As Stream)

これはスタック オーバーフローでの私の最初の投稿です。どうもありがとうございました。

于 2014-04-29T19:31:04.557 に答える
0

Base64 形式でエンコードされた画像を文字列として送信するには、Base64 形式を使用します。

于 2013-10-25T11:44:42.660 に答える