gpt4 book ai didi

c# - 使用 JSON 和 MultipartEntity 将图像从 Android 发送到 WCF

转载 作者:行者123 更新时间:2023-11-30 03:59:31 26 4
gpt4 key购买 nike

因为我的所有其他消息都是 JSON,所以我想我会转换我的 android 解决方案来发送图像,使用 JSON 多部分消息从相机拍摄到 WCF 服务。我想我有发送工作,但不知道如何反序列化。我不使用 base64 编码的原因是我希望 android 2.1 可以工作,而 base64 编码不起作用(至少这是我读过的,而且我发现的唯一“hack”只适用于小文件)。

所以在 android 中我尝试发送图像:

public void upload() throws Exception {
//Url of the server
String url = "http://192.168.0.10:8000/service/UploadImage";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntity mpEntity = new MultipartEntity();
//Path of the file to be uploaded
String filepath = path;
File file = new File(filepath);
ContentBody cbFile = new FileBody(file, "image/jpeg");

//Add the data to the multipart entity
mpEntity.addPart("image", cbFile);
post.setEntity(mpEntity);
//Execute the post request
HttpResponse response1 = client.execute(post);
//Get the response from the server
HttpEntity resEntity = response1.getEntity();
String Response=EntityUtils.toString(resEntity);
Log.d("Response:", Response);

client.getConnectionManager().shutdown();
}

wcf(当我使用 httpurlconnect 和 outputstream 从 android 发送时)代码。它当时正在工作 :D:

public string UploadImage(Stream image)
{
var buf = new byte[1024];
var path = Path.Combine(@"c:\tempdirectory\", "test.jpg");
int len = 0;
using (var fs = File.Create(path))
{
while ((len = image.Read(buf, 0, buf.Length)) > 0)
{
fs.Write(buf, 0, len);
}
}
return "hej";
}

wcf 接口(interface) [操作合约] [网络调用( 方法 = "POST", UriTemplate = "/UploadImage", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string UploadImage(流图像);

如果重要的话,运行 wcf 的控制台应用程序

   static void Main(string[] args)
{
string baseAddress = "http://192.168.0.10:8000/Service";
ServiceHost host = new ServiceHost(typeof(ImageUploadService), new Uri(baseAddress));
WebHttpBinding binding = new WebHttpBinding();
binding.MaxReceivedMessageSize = 4194304;

host.AddServiceEndpoint(typeof(IImageUploadService),binding , "").Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
Console.ReadKey(true);
}

那么现在的问题是,我如何解析传入的 JSON 流?有更好的方法吗?

注意:我尝试设置 fiddler,但 3 小时后甚至无法读取流量,我放弃了。

是否有实际调试此代码的好方法?

如果我将流转换为字节数组并将其保存到文件,则忘记包含结果:

--IZZI8NmDZ-Id7DWP5z0nuPPZspVAGglcfEY9
Content-Disposition: form-data; name="image"; filename="mypicture.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

ÿØÿá°Exif and other funny letters of cause :D ending with
--IZZI8NmDZ-Id7DWP5z0nuPPZspVAGglcfEY9--

通过一些新代码我可以设法得到这个

--crdEqve1GThGGKugB3On0tGNy5h2u746
Content-Disposition: form-data; name="entity"

{"filename":"mypicture.jpg"}
--crdEqve1GThGGKugB3On0tGNy5h2u746
Content-Disposition: form-data; name="file"; filename="mypicture.jpg"
Content-Type: application/octet-stream

ÿØÿá´Exif and the whole image here ...

新的更新例程如下所示:

public void uploadFile() {
String filepath = path;
File file = new File(filepath);
HttpClient httpClient = new DefaultHttpClient();

HttpPost postRequest = new HttpPost("http://192.168.0.10:8000/service/UploadImage");
ResponseHandler<String> responseHandler = new BasicResponseHandler();

// Indicate that this information comes in parts (text and file)
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

try {

//Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();

//Add some values
jsonObj.put("filename", file.getName());

//Add the JSON "part"
reqEntity.addPart("entity", new StringBody(jsonObj.toString()));
}
catch (JSONException e) {
Log.v("App", e.getMessage());
}
catch (UnsupportedEncodingException e) {
Log.v("App", e.getMessage());
}

FileBody fileBody = new FileBody(file);//, "application/octet-stream");
reqEntity.addPart("file", fileBody);

try {
postRequest.setEntity(reqEntity);

//Execute the request "POST"
HttpResponse httpResp = httpClient.execute(postRequest);

//Check the status code, in this case "created"
if(((HttpResponse) httpResp).getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
Log.v("App","Created");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

我仍然需要一种方法来分离流的不同部分,以便我可以划分 json 消息部分(如果我需要那些),然后将图像的字节数组作为单独的部分进行存储。我想我可以跳过 json 并返回到我原来的 JUST 发送图像的字节数组,但是无论如何我都需要能够处理 JSON 消息。

感谢到目前为止的评论。

最佳答案

我的第一个想法是它不是 JSON 流。它可能是一个字节流。此外,如果您的图像大于 1024 字节,您将无限地读取和写入前 1024 字节。您应该有一个偏移量变量来跟踪您阅读了多少内容并在之后开始阅读。

关于c# - 使用 JSON 和 MultipartEntity 将图像从 Android 发送到 WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12736022/

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