gpt4 book ai didi

android - WCF webHttpBinding 与文件元数据

转载 作者:行者123 更新时间:2023-11-30 04:30:20 25 4
gpt4 key购买 nike

我正在使用 Android 客户端将文件上传到 wcf 服务。上传工作正常,但现在我想在请求中获取文件名和另一个整数参数。

我该怎么做? afaik,我不能使用消息协定,因为我没有将消息打包为 SOAP。有替代方案吗?

我在 Android 端使用这段代码:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(serviceAddress +
"/Upload/");
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");

MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("step", new StringBody("1"));
reqEntity.addPart("fileName", new StringBody("elad.jpg"));
reqEntity.addPart("file", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);

和 wcf 端的这段代码(仅用于测试):

 [WebInvoke(UriTemplate = "", Method = "POST", BodyStyle= WebMessageBodyStyle.Bare)]
public void Upload(Stream fileStream)
{
FileStream targetStream = null;
string uploadFolder = @"C:\inetpub\wwwroot\Upload\test.jpg";
using (targetStream = new FileStream(uploadFolder, FileMode.Create,
FileAccess.Write, FileShare.None))
{
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = fileStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
fileStream.Close();
}
}

谢谢!

最佳答案

您可以在 URI 中传递除 Stream 之外的其他参数。帖子在http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx有一个服务的例子。

关于android - WCF webHttpBinding 与文件元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845225/

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