发送图像文件来自于Android的WCF REST服务(Sending Image Files fr

2019-10-23 04:46发布

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

我想从Android发送图片文件,使用WCF REST服务服务器。 但我能够发送图片文件,其他的只有10KB然后,我无法发送。

早期我试图发送Base64编码字符串,但我不能用这个方法来发送。 换了好几配置在WCF的配置文件,但仍然有问题,接收大型文件。

下面是我在那里我有异步执行的Android代码

Android客户端代码中使用WCF REST服务到服务器来发送图像文件

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 REST服务的web.config文件添加配置功能

<?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>

影像服务在WCF接口方法

IWcfAndroidImageService.cs

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

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

   public void GetStream(Stream imageData)
        {
            try
            {
                byte[] buffer = new byte[10000];
                imageData.Read(buffer, 0, 10000);
                FileStream f = new FileStream("D:\\FileUpload\\SubjectFront.JPG", FileMode.OpenOrCreate);
                f.Write(buffer, 0, buffer.Length);
                f.Close();
                imageData.Close();
            }
            catch (Exception ex)
            {
            }
        }

Answer 1:

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

你正在做的只有一次。 所以,你只能得到10000个字节。 其余的丢失。

让你在那里继续读,直到流结束一个循环。

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


Answer 2:

这些下面的代码添加到WCF服务的web.config文件:

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


文章来源: Sending Image Files from Android to WCF REST Service