将图像文件从Android发送到WCF REST服务

时间:2015-05-24 05:36:47

标签: java android image wcf service

我提到了很多解决方案,但没有运气。

我正在尝试使用WCF REST服务将Image文件从Android发送到服务器。但我只能发送10KB的图像文件,除此之外我无法发送。

早期我尝试发送Base64字符串,但我无法使用此方法发送。在WCF配置文件中更改了多个配置,但仍然无法接收大文件。

以下是我的异步执行的Android代码

使用WCF REST服务向服务器发送图像文件的Android客户端代码

[self.navigationItem setHidesBackButton:YES animated:YES];

WCF REST服务中的Web.Config文件以添加配置功能

public void myGoal()
{

       Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.courserequest);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 50, bos);
    byte[] data = bos.toByteArray();
    StringBuilder s;


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

        final String URL1 = "http://localhost:8889/PhotoService/WcfAndroidImageService.svc/GetStream";
        HttpPost httpPost = new HttpPost(URL1);

        ContentBody bin = null;

        httpPost.setEntity(new ByteArrayEntity(data));  



        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中的图像服务接口方法

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true"
      executionTimeout="14400"   />
  </system.web>
  <system.serviceModel>


    <services>
      <service name="WcfAndroidPhotoServis.WcfAndroidImageService" behaviorConfiguration="BehConfig">
        <endpoint address=""
              binding="webHttpBinding"
                 behaviorConfiguration="web"
              contract="WcfAndroidPhotoServis.IWcfAndroidImageService"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8889/PhotoService/WcfAndroidImageService.svc"/>
          </baseAddresses>
        </host>
      </service>
    </services>


    <bindings>
      <webHttpBinding>
        <binding name="WebBinding"
          bypassProxyOnLocal="true"
                 useDefaultWebProxy="false"
                 hostNameComparisonMode="WeakWildcard"
                 sendTimeout="10:15:00"
                 openTimeout="10:15:00"
                 receiveTimeout="10:15:00"
                 maxReceivedMessageSize="2147483647"

                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                        transferMode="Streamed"

                 >

          <readerQuotas maxDepth="128"
          maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

        </binding>
      </webHttpBinding>
    </bindings>




    <behaviors>
      <serviceBehaviors>
        <behavior name="BehConfig" >
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp automaticFormatSelectionEnabled="true"  helpEnabled="true"  defaultOutgoingResponseFormat="Json" />


          <dataContractSerializer maxItemsInObjectGraph="2147483647"  />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

从Android等客户端接收字节流数据的图像服务方法(WcfAndroidImageService.svc.cs)

IWcfAndroidImageService.cs

  [OperationContract]
        [WebInvoke(Method = "POST",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
              //BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "GetStream")]
        void GetStream(Stream imageData);

2 个答案:

答案 0 :(得分:0)

 imageData.Read(buffer, 0, 10000);
 f.Write(buffer, 0, buffer.Length);

你这样做只有一次。所以你只得到10000字节。其余的都丢失了。

制作一个循环,让你继续阅读直到流的结尾。

Int nread = imageData.Read(buffer, 0, 10000);
f.Write(buffer, 0, nread);

答案 1 :(得分:0)

将以下代码添加到wcf服务中的WEB.config文件中:

<bindings>
  <webHttpBinding>
    <binding
      maxBufferPoolSize="2147483647"
      maxReceivedMessageSize="2147483647"
      maxBufferSize="2147483647" transferMode="Streamed">
    </binding>
  </webHttpBinding>
</bindings>