gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:50 24 4
gpt4 key购买 nike

我已经引用了很多解决方案,但没有运气。

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

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

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

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

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

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)
{
}
}

最佳答案

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

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

创建一个循环,继续阅读直到流结束。

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

关于java - 将图像文件从 Android 发送到 WCF REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com